我想將derby DB連接到Java應用程序。我已經下載了所有的罐子,設置類路徑作爲Java目錄,並下載Derby.What我得到現在是:java derby連接到服務器本地主機的錯誤1527
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
誰能幫助我?這是什麼實際上意味着,什麼現在我做的我猜測Database_url變量可能存在問題,但我不知道如何設置它。
private static final String protocol = "jdbc:derby:";
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
final String DATABASE_URL = "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=pass";
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
System.out.println("Loaded the client driver.");
} catch (Exception err) {
System.err.println("Unable to load the client driver.");
err.printStackTrace(System.err);
System.exit(0);
}
String dbName = "BookDatabase";
Connection conn = null;
try {
System.out.println(
"Connecting to and creating the database...");
conn = DriverManager.getConnection(DATABASE_URL);
System.out.println("Database created.");
Statement s = (Statement) conn.createStatement();
s.execute("CREATE TABLE names" +
"(author varchar(50), author_id " +
"int, url varchar(80))");
System.out.println("Created 'names' table.");
System.out.println("Inserting authors.");
s.execute("INSERT INTO names " +
"VALUES ('Adams, Douglas', 1," +
"'http://www.douglasadams.com')");
s.execute("INSERT INTO names " +
"VALUES ('Simmons, Dan', 2, 'http://www.dansimmons.com')");
s.execute("INSERT INTO names " +
"VALUES ('Stephenson, Neal', 3, " +
"'http://www.nealstephenson.com')");
System.out.println("Authors inserted.");
conn.close();
} catch (SQLException err) {
System.err.println("SQL error.");
err.printStackTrace(System.err);
System.exit(0);
}
}