Results 1 to 3 of 3
Thread: Database Connection
- 05-24-2008, 01:21 PM #1
Member
- Join Date
- May 2008
- Posts
- 5
- Rep Power
- 0
Database Connection
Hi im trying to create a database and table from a .class and also want to use Open Office base which i think employs HSQL.
I think its a problem with the drivers.
heres my code
Java Code:import java.io.*; import java.sql.*; public class Presidents { String home, system; public Presidents() { home = System.getProperty("user.home", "."); system = home + File.separatorChar + ".odb"; System.setProperty("hsqldb.system.home", system); } public void createDatabase() { // set the database directory String datasource = "jdbc:hsqldb:presidents;create=true"; try { //load the driver Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection(datasource); Statement st = conn.createStatement(); //create the contacts table int result = st.executeUpdate( "CREATE TABLE contacts (" + "dex INTEGER NOT NULL PRIMARY KEY " + "GENERATED ALWAYS AS identity " + "(START WITH 1, INCREMENT BY 1), " + "name VARCHAR(40), " + "address1 VARCHAR(40), " + "address2 VARCHAR(40), " + "phone VARCHAR(20), " + "email VARCHAR(40))"); //insert a record result= st.executeUpdate( "INSERT INTO contacts (name, address1, address2, " + "phone, email) VALUES(" + "'Jimmy Carter', " + "'Prsidential Center', " + "'680592', " + "'carter@carter.edu')"); st.close(); System.out.println("Database created"); } catch (Exception e) { System.out.println("Error - " + e.toString()); } } public void readDatabase() { String datasource = "jdbc:hsqldb:presidents"; try { //load driver and connect to database Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection(datasource, "",""); Statement st = conn.createStatement(); ResultSet rec =st.executeQuery( "SELECT * FROM contacts"); while(rec.next()) { System.out.println(rec.getString("name") + "\n" + rec.getString("address1") + "\n" + rec.getString("address2") + "\n" + rec.getString("phone") + "\n" + rec.getString("email") + "\n"); } st.close(); } catch (Exception e) { System.out.println("error - " + e.toString()); } } public static void main(String[] arguments) { Presidents prez = new Presidents(); if (arguments[0].equals("create")) { prez.createDatabase(); } if (arguments[0].equals("read")) { prez.readDatabase(); } } }
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
Any help appreciated.Last edited by CompleteBeginner; 05-24-2008 at 02:09 PM. Reason: New Problem?
- 05-24-2008, 02:53 PM #2
have you seen the jdbcDriver.class in the path org/hsqldb/*; ?
freedom exists in the world of ideas
- 05-24-2008, 03:30 PM #3
Member
- Join Date
- May 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
JSP - using connection cache
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:54 AM -
no connection
By even in forum JDBCReplies: 15Last Post: 01-02-2008, 02:50 PM -
Database Connection
By vipinkumarsolanki in forum Advanced JavaReplies: 2Last Post: 11-26-2007, 07:36 AM -
Database connection
By oaklander in forum New To JavaReplies: 2Last Post: 11-13-2007, 12:11 AM -
connection pool for db2
By paty in forum JDBCReplies: 1Last Post: 08-06-2007, 03:43 AM
Bookmarks