Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am the beginner of android.. can anyone help to explain the steps to connect the microsoft access database to android by using eclipse.
Posted

1 solution

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?

SQL
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();
                                }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900