当前位置: 技术问答>java相关
请问关于java连接微软access数据库问题,谢谢!
来源: 互联网 发布时间:2015-06-17
本文导语: 我使用jbuilder6编写程序,连接微软的Ms Access 2000的例子数据库,数据库名称是contact.mdb,我正在学习java,所以有错误,请指正。 我在odbc中建立了数据源contact,然后java代码如下: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); S...
我使用jbuilder6编写程序,连接微软的Ms Access 2000的例子数据库,数据库名称是contact.mdb,我正在学习java,所以有错误,请指正。
我在odbc中建立了数据源contact,然后java代码如下:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strConn = "jdbc:odbc:Contact=C:\Program Files\Microsoft Office\Office\Samples\Contact.mdb;";
strConn = strConn + "Driver ={Microsoft Access Driver (*.mdb)}";
Connection db = DriverManager.getConnection(strConn,"","");
Statement sq_stmt = db.createStatement() ;
String sql_str = "SELECT 姓名,性别 FROM 联系人";
ResultSet rs = sq_stmt.executeQuery(sql_str);
while(rs.next())
{
String name = rs.getString("姓名");
String sex = rs.getString("性别");
System.out.println("姓名"+ name);
System.out.println("性别"+sex);
}
运行时出现的错误信息为:
java.sql.SQLException: [Microsoft][ODBC 驱动程序 管理器] 未发现数据源名称并且未指定默认驱动程序
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6106)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6263)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:2488)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:317)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:160)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at myjdbctest.Frame1.(Frame1.java:34)
at myjdbctest.Application1.(Application1.java:20)
at myjdbctest.Application1.main(Application1.java:49)
我想可能是指定数据库文件的路径的地方有毛病,但是具体是什么原因我不知道,路径是对的。请高手指正,谢谢!
(并请告诉我如何给分)
我在odbc中建立了数据源contact,然后java代码如下:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strConn = "jdbc:odbc:Contact=C:\Program Files\Microsoft Office\Office\Samples\Contact.mdb;";
strConn = strConn + "Driver ={Microsoft Access Driver (*.mdb)}";
Connection db = DriverManager.getConnection(strConn,"","");
Statement sq_stmt = db.createStatement() ;
String sql_str = "SELECT 姓名,性别 FROM 联系人";
ResultSet rs = sq_stmt.executeQuery(sql_str);
while(rs.next())
{
String name = rs.getString("姓名");
String sex = rs.getString("性别");
System.out.println("姓名"+ name);
System.out.println("性别"+sex);
}
运行时出现的错误信息为:
java.sql.SQLException: [Microsoft][ODBC 驱动程序 管理器] 未发现数据源名称并且未指定默认驱动程序
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6106)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6263)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:2488)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:317)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:160)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at myjdbctest.Frame1.(Frame1.java:34)
at myjdbctest.Application1.(Application1.java:20)
at myjdbctest.Application1.main(Application1.java:49)
我想可能是指定数据库文件的路径的地方有毛病,但是具体是什么原因我不知道,路径是对的。请高手指正,谢谢!
(并请告诉我如何给分)
|
public static void main(String[] args){
new StuManager("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:student","","");
}
public StuManager(String driver, String url,String user , String password) {
try{
Class.forName(driver); // Load the driver
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement()
}
catch(ClassNotFoundException cnfe) {
System.err.println(cnfe); // Driver not found
}
catch(SQLException sqle){
System.err.println(sqle); // error connection to database
}
}
new StuManager("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:student","","");
}
public StuManager(String driver, String url,String user , String password) {
try{
Class.forName(driver); // Load the driver
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement()
}
catch(ClassNotFoundException cnfe) {
System.err.println(cnfe); // Driver not found
}
catch(SQLException sqle){
System.err.println(sqle); // error connection to database
}
}