Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problems using prepared statements

 
Ranch Hand
Posts: 41
  • 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="jdbc:odbc: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 ]
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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
  • 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?
 
Ranch Hand
Posts: 547
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks that did it.
 
    Bookmark Topic Watch Topic
  • New Topic