以前我使用此代码连接我的数据库时没有发生错误。
但是来到这个DA文件,它无法连接到数据库。
我已经完成了大部分帖子,但其中一些我不明白。[我只是java的新手]
我曾尝试使用jdbc:derby:// localhost:1527 / societydb; create = true
但同样的错误再次发生。
这是代码和<<<是错误指向的行。
private String host = "jdbc:derby://localhost:1527/societydb";
private String user = "nbuser";
private String password = "nbuser";
private String tableName = "MEMBER";
private void createConnection() {
try {
conn = DriverManager.getConnection(host, user, password);
System.out.println("*** Successfully established the connection to database. ***");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
public ArrayList<Member> getMember() {
ArrayList<Member> memArray = new ArrayList<>();
try {
stmt = conn.prepareStatement("SELECT * FROM " + tableName);//<<< error pointing to here
ResultSet rs = stmt.executeQuery();
while (rs.next()){
Member m = new Member(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getString(10), rs.getString(11));
memArray.add(m);}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error Message", JOptionPane.ERROR_MESSAGE);
}
return memArray;
}
答案 0 :(得分:0)
从Java documentation,您需要的驱动程序是org.apache.derby.jdbc.EmbeddedDriver
和org.apache.derby.jdbc.ClientDriver
。
它也清楚地说明了
Any JDBC 4.0 drivers that are found in your class path are automatically loaded.(However,
you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)
注意:JDBC 4.0从Java 7开始作为默认包提供。
至于您的问题,请在类路径(Ctrl + N in Intellij Idea or Ctrl + R in Eclipse
)中搜索上述类。如果这些类不存在google,请下载并将jar文件添加到类路径中。
答案 1 :(得分:0)
只需添加这些外部罐子:
derby.jar
derbyclient.jar
怎么做:
derby.jar
和derbyclient.jar