ms访问android中的数据库连接 [英] ms access database connectivity in android

查看:57
本文介绍了ms访问android中的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的初学者..任何人都可以帮助解释通过使用eclipse将微软访问数据库连接到android的步骤。

i am the beginner of android.. can anyone help to explain the steps to connect the microsoft access database to android by using eclipse.

推荐答案

利用jtds-1.2.5.jar你可以实现它。你可能知道如何在你的项目中包含jar(将jar复制到项目的lib文件夹中 - > rightclick-> properties-> java build path - > libraries - >导航到你复制的地方说lib文件夹选择jar并点击ok)。写单吨类并制作这个类的实例,并在你的应用程序中访问你需要的地方。



Make use of jtds-1.2.5.jar you can achive it.You may know how to include jar in your project (copy jar into lib folder of your project->rightclick->properties->java build path ->libraries ->navigate to where you have copied say lib folder select jar and hit ok).Write single ton class and make instance of this class and access where ever you need in your application.

public final class Database 
    {
    	
    	public Connection connectionObj;
    	private Statement statement;
    	public static Database dataBase;
    	ResultSet res ;
    	
    	Database() 
    	{
    		String url = "jdbc:jtds:sqlserver://local address or server IP address;"+"databaseName=*****;encrypt=false;";
    		String driver = "net.sourceforge.jtds.jdbc.Driver";
    		String userName = "****";
    		String password = "*****";
    		
    		try 
    		{
    			
    			Class.forName(driver).newInstance();
    			this.connectionObj = (Connection)DriverManager.getConnection(url,userName,password);
    			
    		}
    		catch (Exception sqle) 
    		{
    			sqle.printStackTrace();
    		}
    	}
    	/**
    	 *
    	 * @return MysqlConnect Database connection object
    	 */
    	public static synchronized Database getDbCon() 
    	{
    		if ( dataBase == null ) 
    		{
    			dataBase = new Database();
    		}
    		return dataBase;
    
    	}
    	/**
    	 *
    	 * @param query String The query to be executed
    	 * @return a ResultSet object containing the results or null if not available
    	 * @throws SQLException
    	 */
    	public ResultSet query(String query) throws SQLException
    	{
    		statement = dataBase.connectionObj.createStatement();
    		 res = statement.executeQuery(query);
    		return res;
    	}
    	/**
    	 * @desc Method to insert data to a table
    	 * @param insertQuery String The Insert query
    	 * @return boolean
    	 * @throws SQLException
    	 */
    	public int insert(String insertQuery) throws SQLException 
    	{
    		statement = dataBase.connectionObj.createStatement();
    		int result = statement.executeUpdate(insertQuery);
    		return result;
    
    	}
    
    }





如何使用这个课程?





How to use this class?

Database database = new Database();
                                Database.getDbCon();
        
                                try {
                   database.insert("Insert into tablename(coloum name) values (pass values);
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }


这篇关于ms访问android中的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆