Steps:
1. Now we generally use the compilation environment is the Java SE 1.8, does not support The ODBC connection, so you can use the jdbc connection method, Also download a JDBC driver package on the Web . (Access_jdbc30.jar bag is used here, can be found on the Internet)
2. Right-click on the JRE System libary-> click Build path-> Click Add External jars-> Add Access_jdbc30.jar in.
3. After all these are ready, J builds the database and Access the database version is reduced to - or 2003 the version.
Connect Database code:
Class.forName ("Com.hxtt.sql.access.AccessDriver"). newinstance ();
Connection conn = drivermanager.getconnection ("Jdbc:access:///c:/users/k05/desktop/address.mdb", "" "," ");//database path user name Password
Database Example:
Operation Result:
The code is as follows:
Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importjavax.swing.*;/** * * @version1.22 2017-4-7 *@authorBeimengmuxi*/ Public classastest{ Public Static voidMain (string[] args) {NewAsframe (); }}
ImportJava.sql.*;Importjava.awt.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjavax.swing.*; Public classAsframeextendsjframe{Private Static Final intDefault_weidth = 400; Private Static Final intDefault_height = 680; PrivateJScrollPane Scpdemo; PrivateJTable TabDemo; PrivateJScrollPane ScpDemo1; PrivateJTable TabDemo1; PublicAsframe () {JFrame F=NewJFrame (); F.settitle (Contacts); F.setdefaultcloseoperation (Jframe.exit_on_close); F.setsize (Default_weidth, default_height); F.setvisible (true); F.setresizable (false); F.setlayout (NULL); JLabel Label1=NewJLabel ("Contacts"); F.add (Label1); Label1.setfont (NewFont ("Arial", 1,30)); Label1.setbounds (150, 30, 100, 40); This. Scpdemo =NewJScrollPane (); This. Scpdemo.setbounds (40,110,320,500); Try{btnshow (); } Catch(Instantiationexception |illegalaccessexception e) { //TODO auto-generated Catch blockE.printstacktrace (); } //adding components to a formF.add ( This. Scpdemo); } Public voidBtnshow ()throwsinstantiationexception, illegalaccessexception{String SQL= "SELECT * FROM Address"; Try{ //Get ConnectedClass.forName ("Com.hxtt.sql.access.AccessDriver"). newinstance (); Connection Conn= Drivermanager.getconnection ("Jdbc:access:///c:/users/k05/desktop/address.mdb", "", "" "); PreparedStatement pstm=conn.preparestatement (SQL); ResultSet RS=pstm.executequery (SQL); //calculate how many records there are intCount = 0; while(Rs.next ()) {count++; } RS=Pstm.executequery (); //convert the recorded data obtained by the query into the form of data that is suitable for generating jtableObject[][] Info =NewObject[count][2]; Count= 0; while(Rs.next ()) {info[count][0] = rs.getstring ("name")); info[count][1] = rs.getstring ("PhoneNumber"); Count++; } //Defining a table headerString[] title = {"Name", "Phone number"}; //Create JTable This. TabDemo =NewJTable (Info,title); //Show Table Header//this.jth = This.tabDemo.getTableHeader (); //add jtable to a panel with scroll bars This. Scpdemo.getviewport (). Add (TabDemo); Rs.close (); Conn.close (); }Catch(ClassNotFoundException cnfe) {Joptionpane.showmessagedialog (NULL, "Data source Error", "Error", Joptionpane.error_message); }Catch(SQLException sqle) {Joptionpane.showmessagedialog (NULL, "Data manipulation Error", "Error", Joptionpane.error_message); } }}
Java connection to an Access database