Codejoy Newbie

Joined: 19 Nov 2005 Posts: 4
|
Posted: Sat Nov 19, 2005 11:22 pm Post subject: lost, built db fine but now want my java to connect to it. |
|
|
So I am totally lost, I built the odb just fine, added my tables, the fields the primary keys just fine. Now I have no clue how to connect to it, I got some sample code here:
| Code: |
/*
* Main.java
*
* Created on November 18, 2005, 4:17 PM
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package studiosnap;
//when a client is made, and a session created, you can op to at any point after that import a folder of jpg's
//into the folder, which basically consists of copying the jpg's to the right folder and building up the catalog
//file.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Shane Thomas
*/
public class Main {
private Connection con;
private ResultSet rs;
private Statement st;
/** Creates a new instance of Main */
public Main() {
}
public void createConnection() {
try {
System.out.print ("Connecting to database...");
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:studiosnap2");
st = con.createStatement();
System.out.println("\t[DONE]");
} catch (Exception err) {
System.err.println("\nERROR: failed to load HSQLDB JDBC driver.");
err.printStackTrace();
System.exit(1);
}
}
public void retrieveRecords() {
try {
rs = st.executeQuery("SELECT * FROM Person");
System.out.println("\t\\n");
while(rs.next())
System.out.println(rs.getString("lastName") + ",\t\t" + rs.getString("firstName"));
} catch(SQLException err) {
System.err.println("\nERROR: failed to retrieve records.");
err.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
Main app = new Main();
app.createConnection();
app.retrieveRecords();
}
}
|
Alas, this doesn't work, it can tell when the file is locked though (ifi have it open) other than that i get the error:
ERROR: failed to retrieve records.
java.sql.SQLException: Table not found in statement [SELECT * FROM Person]
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.jdbcStatement.executeQuery(Unknown Source)
at studiosnap.Main.retrieveRecords(Main.java:5
at studiosnap.Main.main(Main.java:74)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
so im pretty well lost, any sorta of inkling would help?i saw this:
http://www.linuxquestions.org/questions/showthread.php?threadid=377260
but doing what got this working for the poster (5th post down) was confusing, and trying it that way it wanted me to create a whole new database (for testing purposes i did). but after creating a table with fields, i couldnt edit the primary key to say auto, and then bringing up the table itsel fdidnt allow me to enter test data.
Needless to say i am in the dark...
any suggestions? links? code snippets? help of anykind. |
|