I create a java desktop application using Netbeans that uses the embedded derby
driver. it works fine as long as i connect to the database manually within
Netbeans. If I don't, i get a return code of 8000/4001 connection refused.
I created a project in Netbeans using the derby demo application too, so i
could see if the problem was my code or something else. I have the same
problem. it only works if i right click on the database in Netbeans and
choose "connect".
i seem to be missing something fundamental here.
can anybody help?
package ondemand;
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 DemandDatabase {
private String framework = "embedded";
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String protocol = "jdbc:derby:";
public DemandDatabase() {
System.out.println("in constructor ");
}
public ArrayList go()
{
System.out.println("about to load the driver in " + framework + "
mode");
loadDriver();
Connection conn = null;
ArrayList statements = new ArrayList();
ArrayList jobnames = new ArrayList();
Statement s = null;
ResultSet rs = null;
try {
Properties props = new Properties(); // connection properties
props.put("user", "nbuser");
props.put("password", "nbuser");
String dbName = "//localhost:1527/users"; // the name of the
database
conn = DriverManager.getConnection(protocol + dbName
+ ";create=false", props);
System.out.println("Connected to database " + dbName);
conn.setAutoCommit(true);
s = conn.createStatement();
statements.add(s);
rs = s.executeQuery(
"SELECT JOBNAME, USERID FROM APP.JOBS");
String txtJobname;
String txtUserID;
while (rs.next()) {
txtJobname = rs.getString("JOBNAME");
System.out.println("value of jobname is " + txtJobname);
txtUserID = rs.getString("USERID");
System.out.println("value of User ID is " + txtUserID);
jobnames.add(txtJobname);
}
try {
// the shutdown=true attribute shuts down Derby
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
if (((se.getErrorCode() == 50000)
&& ("XJ015".equals(se.getSQLState())))) {
// we got the expected exception
System.out.println("Derby shut down normally");
// Note that for single database shutdown, the expected
// SQL state is "08006", and the error code is 45000.
} else {
// if the error code or SQLState is different, we have
// an unexpected exception (shutdown failed)
System.out.println("Derby did not shut down normally");
printSQLException(se);
}
}
} catch (SQLException sqle) {
printSQLException(sqle);
} finally {
// release all open resources to avoid unnecessary memory usage
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
// Statements and PreparedStatements
int i = 0;
while (!statements.isEmpty()) {
// PreparedStatement extend Statement
Statement st = (Statement) statements.remove(i);
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
}
//Connection
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
}
return jobnames;
}
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 reportFailure(String message) {
System.err.println("\nData verification failed:");
System.err.println('\t' + message);
}
public static void printSQLException(SQLException e) {
// Unwraps the entire exception chain to unveil the real cause of the
// Exception.
while (e != null) {
System.err.println("\n----- SQLException -----");
System.err.println(" SQL State: " + e.getSQLState());
System.err.println(" Error Code: " + e.getErrorCode());
System.err.println(" Message: " + e.getMessage());
// for stack traces, refer to derby.log or uncomment this:
//e.printStackTrace(System.err);
e = e.getNextException();
}
}
}