• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

database

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to connect MS-Access database in java. I have windows_xp.but jdbc odbc driver didnot connect properly in my system please give the suggestion in my problem.
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jdbc odbc driver didnot connect properly

It should work. Can you please tell us what you tried, and how it fails?

Regards, Jan
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a database connection using JDBC involves 3 main steps.
1. Loading the database driver
2. Getting the connection using the driver manager
3. Using the connection to access the underlying database.

The sample code below explains the steps in detail.

import java.sql.*;

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try{
//Load the database driver class
Class.forName("your databse driver name");
//Example, mySql driver is com.mysql.jdbc.Driver

//Get the connection
conn = DriverManager.getConnection( "dburl","user","password") ;

//Create a Statement object.
stmt = conn.createStatement();

//Get a ResultSet

rs = stmt.executeQuery("your query here");

//Loop through the results
while(rs.next()){
//get to each column in a row.
//for example to get a value from a column named 'user'
String userName = rs.getString("user");
}
}catch( Throwable t){
//handle error here
}
finally{

//note the sequence of closing the objects. rs first followed
// by stmt and connection objects
try{
if(rs != null) rs.close();
}catch(Throwable t){t.printStackTrace();}

try{
if(stmt != null) stmt.close();
}catch(Throwable t){t.printStackTrace();}

try{
if(con != null) con.close();
}catch(Throwable t){t.printStackTrace();}
}


Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The one stop resource for all your Java questions and answers.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will tell you step by step procedure of how to create table in MS Access and example of JDBC using java

First create table in Access as

Microsoft Access-File-New-Blank database-Enter Filename as mydb1-Select path to store-OK-Double Click Create table in design view-Enter EmpID as field name and data type as number & enter EmpName as field name and data type as text-Right click first row and click primary key-Save as EmpTable-OK - close

Ereation of EmpDSN
Control panel-Administrative tools-Data sources (ODBC)-click MS Access DataBase-Add-select microsoft access driver (*.mdb)-Finish-Enter data Source name as EmpDSN-Select mydb1.mdb from location where you stored - OK.

Now check this java file to access database

// file name myEmployee.java

import java.sql.*;

public class myEmployee
{
public static void main(String args[]) throws SQLException
{
Connection con;
Statement stmt;
String query = "select * from EmpTable";
String updateString = "INSERT INTO EmpTable VALUES (4,'pankaj')";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.print("ClassNotFoundException: " + e);
}
con = DriverManager.getConnection("jdbc:odbc:EmpDSN");
stmt = con.createStatement();
stmt.executeUpdate(updateString);
ResultSet rs = stmt.executeQuery(query);
System.out.println("Employee Details : ");
while (rs.next())
{
int empID = rs.getInt(1);
String empName = rs.getString(2);
System.out.println(empID + " " + empName);
}
stmt.close();
con.close();
}
}

Now compile and run this file. And check EmpTable in database Mydb1. You will see values are inserted in table.
Remember when you run this example second time first delete inserted record from EmpTable, and run example.
For details check tutorial on JDBC.

Pankaj Shinde

[ January 19, 2008: Message edited by: Pankaja Shinde ]
[ January 19, 2008: Message edited by: Pankaja Shinde ]
reply
    Bookmark Topic Watch Topic
  • New Topic