为 EmbeddedDerby 加载 JDBC 驱动程序时出现问题。以下是我编译和运行程序的案例
情况1:
编译:E:\java\WorkReminder>javac -d class source/MyDerbyProgram.java
运行:E:\java\WorkReminder>java -cp class MyDerbyProgram
错误:
无法加载 JDBC 驱动程序 org.apache.derby.jdbc.EmbeddedDriver 请检查您的 CLASSPATH。java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader。 findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) at java.lang.ClassLoader.loadClass(ClassLoader .java:251) 在 java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) 在 java.lang.Class.forName0(Native Method) 在 java.lang.Class.forName(Class.java:164) 在 MyDerbyProgram。 loadDriver(MyDerbyProgram.java:143) 在 MyDerbyProgram.go(MyDerbyProgram.
案例2:如果我这样做,一切都好
编译:E:\java\WorkReminder>javac -d class source/MyDerbyProgram.java
更改目录:E:\java\WorkReminder>cd class
运行:E:\java\WorkReminder\class>java MyDerbyProgram
输出:
加载适当的驱动程序
插入的行。
2-----
1956_ _ __ _ __ _ +__ _ __ _ __ _ ___ _河内
1975_ _ __ _ __ _ _ +__ _ __ _ __ _ ___ _西贡
SimpleApp 完成
有人可以帮我解释为什么我在案例 1 中出现错误,因为我正在编写一个更复杂的程序吗?我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;
public class MyDerbyProgram
{
/* the default framework is embedded*/
private String framework = "embedded";
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String protocol = "jdbc:derby:";
public static void main(String[] args)
{
new MyDerbyProgram().go(args);
System.out.println("SimpleApp finished");
}
void go(String[] args)
{
/* load the desired JDBC driver */
loadDriver();
try
{
Connection connection = DriverManager.getConnection(protocol + "testDB; create=true");
connection.setAutoCommit(false);
//Create table
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
statement.execute("create table location(num int, address varchar(40))");
//Insert
PreparedStatement psInsertStatement = connection.prepareStatement("insert into location values(?, ?)");
psInsertStatement.setInt(1, 1956);
psInsertStatement.setString(2, "Ha Noi");
psInsertStatement.executeUpdate();
psInsertStatement.setInt(1, 1975);
psInsertStatement.setString(2, "Sai Gon");
psInsertStatement.executeUpdate();
System.out.println("Row inserted.");
//Select
ResultSet resultSet = statement.executeQuery("select * from location");
int totalRows = 0;
resultSet.last();
totalRows = resultSet.getRow();
resultSet.beforeFirst();
System.out.println(totalRows + "-----");
String strPrintResult = "";
while(resultSet.next())
{
strPrintResult += resultSet.getString("num") + "___________________+_________________________" + resultSet.getString("address") + "\n";
}
System.out.println(strPrintResult);
//Drop table
statement.execute("drop table location");
//Commit transaction
connection.commit();
//Close
statement.close();
statement = null;
psInsertStatement.close();
psInsertStatement = null;
resultSet.close();
resultSet = null;
connection.close();
connection = null;
}
catch(SQLException se)
{
System.out.println(se.toString());
}
}
private void loadDriver() {
try {
Class.forName(driver).newInstance();
System.out.println("Loaded the appropriate driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("\nUnable to load the JDBC driver " + driver);
System.err.println("Please check your CLASSPATH.");
cnfe.printStackTrace(System.err);
} catch (InstantiationException ie) {
System.err.println(
"\nUnable to instantiate the JDBC driver " + driver);
ie.printStackTrace(System.err);
} catch (IllegalAccessException iae) {
System.err.println(
"\nNot allowed to access the JDBC driver " + driver);
iae.printStackTrace(System.err);
}
}
private void parseArguments(String[] args)
{
if (args.length > 0) {
if (args[0].equalsIgnoreCase("derbyclient"))
{
framework = "derbyclient";
driver = "org.apache.derby.jdbc.ClientDriver";
protocol = "jdbc:derby://localhost:1527/";
}
}
}
}
问候