File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems using prepared statements

 
Felipe Walker
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Felipe,
What is the return value of ps.executeUpdate()?
 
Felipe Walker
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks that did it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic