I am fairly new to Java, so I bought a book to learn from. Everything went swimmingly until I got to the chapter on SQL. I am working in NetBeans with the sample database, but I can't get the database to connect with the program.
我对Java很陌生,所以我买了一本书来学习。一切都进行得很顺利,直到我讲到SQL这一章。我正在使用样例数据库使用NetBeans,但是我无法让数据库与程序连接。
Here's the code I faithfully copied from the book:
这是我从书中忠实复制的代码:
import java.sql.*;
public class SysTableReporter {
public static void main(String[] arguments) {
String data = "jdbc:derby://localhost:1527/sample";
try (
Connection conn = DriverManager.getConnection(
data, "app", "APP");
Statement st = conn.createStatement()) {
System.out.println("TABLEID:\t" );
Class.forName("org.apache.derby.jdbc.ClientDriver");
ResultSet rec = st.executeQuery(
"select * " +
"from SYS.SYSTABLES " +
"order by TABLENAME");
while(rec.next()) {
System.out.println("TABLEID:\t" + rec.getString(1));
System.out.println("TABLENAME:\t" + rec.getString(2));
System.out.println("TABLETYPE:\t" + rec.getString(3));
System.out.println("SCHEMAID:\t" + rec.getString(4));
System.out.println();
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
}
}
}
Here's what my Services Panel looks like:
我的服务面板是这样的:
点击查看图片
Here's my output:
这是我的输出:
SQL Error: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/sample 0 08001
At first I figured I just had some typo, but looking over it carefully I can't find it. I tried installing a new JDBC driver, but that didn't seem to help either. I'm running a Windows 8 laptop, and I have the telnet client turned on. My best guess at this point is that I somehow missed a step in the initial setup, but I can't find it to go back and fix it. Any help would be great.
起初我以为我只是打错了字,但仔细看了看,我找不到。我尝试安装一个新的JDBC驱动程序,但这似乎也没有帮助。我正在运行Windows 8笔记本电脑,我打开了telnet客户端。我目前的最佳猜测是,我在初始设置中不知何故漏掉了一个步骤,但是我找不到它来返回并修复它。任何帮助都是伟大的。
2 个解决方案
#1
2
You are probably just missing the Derby JDBC driver jar in your project's library section (I'm assuming that you created a standard Netbeans project, not a Maven type project). The jar is called derbyclient.jar.
您可能只是在项目的库部分丢失了Derby JDBC驱动程序jar(我假设您创建了一个标准的Netbeans项目,而不是Maven类型的项目)。这个jar被称为derbyclient.jar。
The easiest way to solve this:
最简单的解决方法是:
- locate derbyclient.jar eg with Explorer
- 定位derbyclient。jar如与探险家
- in Netbeans, rightclick on the project node in the Projects pane.
- 在Netbeans中,右键单击Projects窗格中的项目节点。
- Select Properties, and there select libraries
- 选择属性,然后选择库
- Click "Add JAR/Folder", navigate to derbyclient.jar
- 单击“添加JAR/文件夹”,导航到derbyclient.jar
That effectively adds the jar to your project. Just recompile, run and everything should work as intended.
这有效地将jar添加到项目中。只要重新编译、运行,一切都应该按照预期运行。
EDIT: aside from @BobKuhar's find, another problem with the given code is that it doesn't use one of Java's more powerful debugging mechanisms, the stacktrace. At its most basic form, showing them on screen is simple, not more than
编辑:除了@BobKuhar的发现之外,给定代码的另一个问题是它没有使用Java更强大的调试机制之一stacktrace。在最基本的形式中,在屏幕上显示它们是很简单的
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
// and show us the stacktrace
s.printStackTrace();
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
// and show us the stacktrace
e.printStackTrace();
}
the stack trace will not only show you the exact line at which the error occurred, but also the trajectory to the exception (how the program got there), invaluable in more complicated programs. Definitely something you want to learn to use!
堆栈跟踪不仅会显示错误发生的确切路线,还会显示异常的轨迹(程序是如何到达的),这在更复杂的程序中是非常有用的。绝对是你想学的东西!
Lots of info on stack traces here: What is a stack trace, and how can I use it to debug my application errors?
这里有很多关于堆栈跟踪的信息:什么是堆栈跟踪,我如何使用它来调试应用程序错误?
#2
1
I think what you really have is just a sequencing problem. The Class.forName call registers the driver with the DriverManager (I think). This needs to occur before you attempt to establish a Connection through the DriverManager.
我认为你真正拥有的只是一个排序问题。类。forName call向驱动程序管理器注册驱动程序(我认为)。这需要在尝试通过DriverManager建立连接之前进行。
Class.forName( "org.apache.derby.jdbc.ClientDriver" ).newInstance();
Connection conn = DriverManager.getConnection( data, "app", "APP");
If this gives you some "ClassNotFound" exception, then fvu's assertion that you don't have the Derby JDBC Jar on the class path is your next issue.
如果这给了您一些“ClassNotFound”异常,那么fvu断言您在类路径上没有Derby JDBC Jar是下一个问题。
The Derby docs have an example: http://db.apache.org/derby/integrate/plugin_help/derby_app.html
Derby文档有一个示例:http://db.apache.org/derby/integrate/plugin_help/derby_app.html
#1
2
You are probably just missing the Derby JDBC driver jar in your project's library section (I'm assuming that you created a standard Netbeans project, not a Maven type project). The jar is called derbyclient.jar.
您可能只是在项目的库部分丢失了Derby JDBC驱动程序jar(我假设您创建了一个标准的Netbeans项目,而不是Maven类型的项目)。这个jar被称为derbyclient.jar。
The easiest way to solve this:
最简单的解决方法是:
- locate derbyclient.jar eg with Explorer
- 定位derbyclient。jar如与探险家
- in Netbeans, rightclick on the project node in the Projects pane.
- 在Netbeans中,右键单击Projects窗格中的项目节点。
- Select Properties, and there select libraries
- 选择属性,然后选择库
- Click "Add JAR/Folder", navigate to derbyclient.jar
- 单击“添加JAR/文件夹”,导航到derbyclient.jar
That effectively adds the jar to your project. Just recompile, run and everything should work as intended.
这有效地将jar添加到项目中。只要重新编译、运行,一切都应该按照预期运行。
EDIT: aside from @BobKuhar's find, another problem with the given code is that it doesn't use one of Java's more powerful debugging mechanisms, the stacktrace. At its most basic form, showing them on screen is simple, not more than
编辑:除了@BobKuhar的发现之外,给定代码的另一个问题是它没有使用Java更强大的调试机制之一stacktrace。在最基本的形式中,在屏幕上显示它们是很简单的
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
// and show us the stacktrace
s.printStackTrace();
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
// and show us the stacktrace
e.printStackTrace();
}
the stack trace will not only show you the exact line at which the error occurred, but also the trajectory to the exception (how the program got there), invaluable in more complicated programs. Definitely something you want to learn to use!
堆栈跟踪不仅会显示错误发生的确切路线,还会显示异常的轨迹(程序是如何到达的),这在更复杂的程序中是非常有用的。绝对是你想学的东西!
Lots of info on stack traces here: What is a stack trace, and how can I use it to debug my application errors?
这里有很多关于堆栈跟踪的信息:什么是堆栈跟踪,我如何使用它来调试应用程序错误?
#2
1
I think what you really have is just a sequencing problem. The Class.forName call registers the driver with the DriverManager (I think). This needs to occur before you attempt to establish a Connection through the DriverManager.
我认为你真正拥有的只是一个排序问题。类。forName call向驱动程序管理器注册驱动程序(我认为)。这需要在尝试通过DriverManager建立连接之前进行。
Class.forName( "org.apache.derby.jdbc.ClientDriver" ).newInstance();
Connection conn = DriverManager.getConnection( data, "app", "APP");
If this gives you some "ClassNotFound" exception, then fvu's assertion that you don't have the Derby JDBC Jar on the class path is your next issue.
如果这给了您一些“ClassNotFound”异常,那么fvu断言您在类路径上没有Derby JDBC Jar是下一个问题。
The Derby docs have an example: http://db.apache.org/derby/integrate/plugin_help/derby_app.html
Derby文档有一个示例:http://db.apache.org/derby/integrate/plugin_help/derby_app.html