我学习JDBC连接已有几天,最近使用h2关系数据库尝试了此代码。我收到一条消息,指出驱动程序不可用,我已经检查过了,驱动程序在eclipse软件包的lib文件夹中应该存在的位置。我应该怎么办?
package jdbcwork;
//importing java commands
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Manager1
{
//main method with exception throw
public static void main(String[] args) throws ClassNotFoundException
{
//try block
try
{
//calling the h2 database driver org.h2.Driver
Class.forName("org.h2.Driver");
//declaring host ,username and password
String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
String uName="sa";
String uPass="sa";
//giving a connection to the h2 database driver driver
Connection conn = DriverManager.getConnection("h2","sa","sa");
}
//catch block
catch(SQLException err)
{
//printing an error message
System.out.println(err.getMessage());
}
}
}
//declaring host ,username and password
String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
String uName="sa";
String uPass="sa";
//giving a connection to the h2 database driver driver
Connection conn = DriverManager.getConnection("h2","sa","sa");
更改如下:
//declaring host ,username and password
String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
String uName="sa";
String uPass="sa";
//giving a connection to the h2 database driver driver
Connection conn = DriverManager.getConnection(host,uName,uPass);
并确保 h2.jar
必须进入您的类路径
如果要下载检查此:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句