| Author |
database
|
vani geetha
Greenhorn
Joined: Jan 18, 2008
Posts: 2
|
|
|
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.
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2343
|
|
jdbc odbc driver didnot connect properly
It should work. Can you please tell us what you tried, and how it fails? Regards, Jan
|
OCUP UML fundamental
ITIL foundation
|
 |
Lukasz Bajzel
Greenhorn
Joined: Dec 03, 2007
Posts: 26
|
|
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.
|
 |
Pankaja Shinde
Ranch Hand
Joined: Sep 15, 2006
Posts: 61
|
|
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 dbc: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 ]
|
 |
 |
|
|
subject: database
|
|
|