This is an example of a Connection between Java and HSQLDB database. You'll also need to download HSQLDB driver from their home page an include it in Java Project Properties to make this work.
http://hsqldb.org/
public class JCdal {
private static String db = "xpto";
private static Connection cn;
private static Statement st;
private static ResultSet rs;
/* Constructor */
public JCdal()
{
}
//Connect to the Database ------------------------------------------
public static void getConnection() {
try {
JOptionPane.showMessageDialog(null, "0");
Class.forName("org.hsqldb.jdbcDriver");
JOptionPane.showMessageDialog(null, "1");
cn= DriverManager.getConnection("jdbc:hsqldb:" + db,"sa","");
JOptionPane.showMessageDialog(null, "2");
st = cn.createStatement();
JOptionPane.showMessageDialog(null, "3");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error Connecting to Database(getconnection)!");
}
}
//Close Database Connection ------------------------------------------
public static void closeConnection() {
try {
rs.close();
st.execute("SHUTDOWN");
cn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error Closing Database!");
}
}
public Statement getStatement() {
return this.st;
}
public Connection getConnectCN() {
return this.cn;
}
}
Tuesday, January 13, 2009
Data Access Layer Class DAL (JAVA - HSQLDB)
Subscribe to:
Post Comments (Atom)
0 comentários:
Post a Comment