this is the continuation of my previous question How to configure javadb in eclipse?. I was able to made some changes on my code but now it is giving me this error -> ERROR: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Hope you guys can help me with this one.
Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
final String DB_URL = "jdbc:derby://localhost:1527/CoffeeDB;create=true";
try{
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement();
String sql = ("CREATE TABLE Coffee(Description CHAR(25),ProdNum CHAR(10) NOT NULL PRIMARY KEY,Price DOUBLE)");
stmt.execute(sql);
sql = "INSERT INTO Coffee VALUES('Bolivian Dark','14-001',8.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Bolivian Medium','14-002',8.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Brazilian Dark','15-001',7.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Brazilian Medium','15-002',7.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Brazilian Decaf','15-003',8.55)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Central American Dark','16-001',9.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Central American Medium','16-002',9.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Sumatra Dark','17-001',7.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Sumatra Decaf','17-002',8.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Sumatra Medium','17-003',7.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Sumatra Organic Dark','17-004',11.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Kona Medium','18-001',18.45)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Kona Dark','18-002',18.45)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('French Roast Dark','19-001',9.65)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Galapagos Medium','20-001',6.85)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Guatemalan Dark','21-001',9.95)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Guatemalan Decaf','21-002',10.45)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Coffee VALUES('Guatemalan Medium','21-003',9.95)";
stmt.executeUpdate(sql);
String sqlStatement = "SELECT Description FROM Coffee";
ResultSet result = stmt.executeQuery(sqlStatement);
System.out.println("Coffees found in the Database");
System.out.println("-----------------------------");
while(result.next()){
System.out.println(result.getString("Description"));
}
conn.close();
}catch(Exception ex){
System.out.println("ERROR: " + ex.getMessage());
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…