//package Answer;

import java.sql.*;

class Answercheck 
{
public static String correct;
public static String user="Brandon";

/*

public static void check(int q1)
{

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "",""); 



Statement s = con.createStatement();
s.execute("SELECT copt FROM Ques WHERE ID=" +q1);

ResultSet rs = s.getResultSet();




if (rs != null) // if rs == null, then there is no ResultSet to view

while ( rs.next() ) // this will step through our data row-by-row
{

correct = rs.getString("copt");

//System.out.println("CORRECT OPTION by Answercheck"+correct + "\n");

}  //while loop ends

}   //try block


catch (Exception err)
{
System.out.println( "Error: " + err );
}

}  // function ends


*/

public static void res(int m1)
{

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "",""); 



Statement s = con.createStatement();
s.executeUpdate("INSERT INTO Final " +"(candidate, marks) "+" VALUES "+"(' "+ user +" ' , ' " +m1 +" ' )"); 

//ResultSet ps = s.getResultSet();

System.out.println("Is it done??");

}


catch (Exception err)
{
System.out.println( "Error: " + err );
}

}  // function ends










public static void main(String x[])
{
//check(1);
res(4);
}



} //class ends

have a look at the Insert sql command....

It runs perfectly...But it wont insert the values into the Database... Ive checked the Database...its allright...but no value is inserted into it

what am i dong wrong?

You might want to post this question in one of the database forums if you don't get an answer here.

Do you get any errors? Place some debug messages and see what happens. The best thing you can do is print the query that you run and try to run at the database.

public static void res(int m1)
{

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "",""); 

String query = "INSERT INTO Final " +"(candidate, marks) "+" VALUES "+"(' "+ user +" ' , ' " +m1 +" ' )";

System.out.println("Query:>"+query+"<");

Statement s = con.createStatement();
int i = s.executeUpdate(query); 
System.out.println("Rows updated: "+i);
//ResultSet ps = s.getResultSet();

System.out.println("Is it done??");

}


catch (Exception err)
{
err.printStackTrace();
System.out.println( "Error: " + err.getMessage() );
}

}  // function ends

After you have made those changes to your code, here are your mistakes.
You don't close anything!!!! You must close whatever you open. In order to do that you need to close them in a finally block to make sure that they are always closed:

finally {
   con.close();
   s.close(); // the statement
}

But since those are declared inside the try the above code will not work because they are not visible outside it. Also the close operation also throws an exception so you need to put that in a try-catch as well:

public static void res(int m1)
{

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

// Declared outside the try so they can be used inside the try and the finally
Connection con = null;
Statement st = null;
// But you initialize them in the try:
try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dbURL, "",""); 

String query = "INSERT INTO Final " +"(candidate, marks) "+" VALUES "+"(' "+ user +" ' , ' " +m1 +" ' )";

System.out.println("Query:>"+query+"<");

st = con.createStatement();
int i = st.executeUpdate(query); 
System.out.println("Rows updated: "+i);

} catch (Exception err) {
  err.printStackTrace();
  System.out.println( "Error: " + err.getMessage() );
} finally {
   try { if (st!=null) st.close(); } catch (Exception e){}
   try { if (con!=null) con.close(); } catch (Exception e){}
}

}  // function ends

In the finally you close what you opened.
You used the try-catch in the finally: try { ... } catch (Exception e){} Because the close operation also throws an exception and you need to catch that as well. But you don't need to handle it so the catch is empty.

You check if they are null: if (st!=null) st.close(); because if something goes wrong, they might not take value in the try, since they are declared null outside the try. They cannot be initialized outside the try because that would throw an exception. So they are declared outside, initialized inside the try and in the finally you close them only if the are not null, meaning if they have been "opened" (initialized)


EDIT: The query also needs to change. It must be like this: VALUES "+"('"+ user +" NOT: VALUES "+"(' "+ user +" Check the empty space, when you print it. Otherwise instead of saving this in the DB: "Brandon", you would be saving this: " Brandon "
Better have it like this: String query = "INSERT INTO Final (candidate, marks) VALUES ('" + user + "' , '" + m1 + "')";

Edited 6 Years Ago by javaAddict: n/a

This question has already been answered. Start a new discussion instead.