Home >Computer Tutorials >Computer Knowledge >How to create a database using Java and Access
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class connODBC {
public static Statement stmt;
public static ResultSet rs;
String sql;
public static void main(String[] args) {
connODBC task=new connODBC();
task.ConnectionODBC();
}
public void ConnectionODBC() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=table.mdb";
Connection con = DriverManager.getConnection(url);
System.out.println ("Data source connection successful...");
stmt=con.createStatement();
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
Note: The table.mdb file must be in the directory where the .java file is located and must exist.
Answer class tes {
private String url;
private Connection con=null;
public tes() throws ClassNotFoundException,SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDrier");
url="jdbc:odbc:Driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=E:/java/workspace/BankSystem/src/bank.accdb";
con=DriverManager.getConnection(url);
System.out.println("ok");
}
public static void main(String[] args){
try {
new tes();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The following means that there is a problem with your driver, please take a closer look.
The above is the detailed content of How to create a database using Java and Access. For more information, please follow other related articles on the PHP Chinese website!