aspose file tools*
The moose likes JDBC and the fly likes problems using prepared statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problems using prepared statements" Watch "problems using prepared statements" New topic
Author

problems using prepared statements

Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
I am trying to use prepared statements connecting to a access database, I don't get any errors it just wont insert into my table.

when I use statement, it works fine just prepared statements won't work. The code for the databaseapp and the test class is below.

class DatabaseAppTest {
public static void main(String[] args){
DatabaseApp datest = new DatabaseApp();
datest.runDatabase("test","test","test","test","test","test","test");
}
}


import java.sql.*;

class DatabaseApp{
private Connection c;
private String url="jdbcdbc:SCDowntime";
private String database = "SCDowntime";
private Statement stmt;
private PreparedStatement ps;
private static String createTableSCDowntime = "CREATE TABLE TICKETS" + "(NODE VARCHAR(32), USER_ID VARCHAR(32), NAME VARCHAR(32), CONTACT VARCHAR(32), DETAILS VARCHAR(250), RESOLUTION VARCHAR(250), ENTER_DATE VARCHAR(20))";

public DatabaseApp(){}

public void runDatabase(String newNode, String newUserid, String newCus, String newContact, String newDetails, String newRes, String newDate){

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
c=DriverManager.getConnection(url);
stmt =c.createStatement();
stmt.executeUpdate(createTableSCDowntime);

stmt.executeUpdate("INSERT INTO TICKETS " + "VALUES ('JH1234000','XCABC','Customer','contact','This are my details','this is my resolution','12/16/2004')");

ps = c.prepareStatement("INSERT INTO TICKETS" + " VALUES (?,?,?,?,?,?,?)");
ps.setString(1,newNode);
ps.setString(2,newUserid);
ps.setString(3,newCus);
ps.setString(4,newContact);
ps.setString(5,newDetails);
ps.setString(6,newRes);
ps.setString(7,newDate);
ps.executeUpdate();

}
catch(SQLException exception){
System.out.println("Database Error: " + exception);
}
catch(ClassNotFoundException e){
System.out.println("class not found Error: " + e);
}
catch (Exception ex){
System.out.println("Error: " + ex);
}
}
}





[edited to disable smilies]
[ December 27, 2004: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30352
    
150

Felipe,
What is the return value of ps.executeUpdate()?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
don't have one is that the problem.
This is the first time trying to use prepared statements.
What should the correct format look like?
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi

i dont know if this will solve your problem but always close the Statements, PreparedStatements and Connections:


otherwise you will run into resource problems and will have to many open connections/cursors/resultssets/statements.

pascal
Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41

Thanks that did it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems using prepared statements