JDBC-ODBC database related

someone please help me with this issue. i wonder why i need to put 2 lines of executeUpdate() to successfully insert a new record... thanks in advance for those who helped ;)

peixing.

import java.io.*;

publicclass Index{

publicstaticvoid main(String[] args)throws IOException{

BufferedReader input =new BufferedReader(new InputStreamReader(System.in));

String userId ="";

System.out.println("Welcome to New World Inc.");

System.out.println("1. Create new user");

int choice = 0;

do{

System.out.print("Please select an option: ");

try{

choice = Integer.parseInt(input.readLine());

}

catch(NumberFormatException e){

//e.printStackTrace();

choice = 0;

}

}while(choice<1 || choice>3);

if(choice==1){

Account acc =new Account();

do{

System.out.print("Enter user name: ");

try{

userId = input.readLine();

acc.setUserId(userId);

}

catch(Exception e){

e.printStackTrace();

}

}while(userId.equals(""));

}

}

}

import java.sql.*;

publicclass Account{

DBC dbc;

public Account(){

dbc =new DBC();

dbc.setUp("database");

}

publicvoid setUserId(String userId){

int status = dbc.updateRequest("INSERT INTO Account VALUES('" + userId +"','','')");

dbc.updateRequest("INSERT INTO Account VALUES('" + userId +"','','')");

//<===== i wonder why i need to write an additional updateRequest then i am able to insert a new record into my database....

//its so weird. and everytime i take away this additional method, i cannot add a new record into my database

}

}

import java.sql.*;

publicclass DBC{

private Connection con;

publicvoid setUp(String dsn){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(Exception e){

System.out.println("Load driver error");

}

try{

String s ="jdbc:odbc:" + dsn;

con = DriverManager.getConnection(s,"","");

}

catch(Exception e){

e.printStackTrace();

}

}

publicint updateRequest(String dbQuery){

int count=0;

try{

Statement stmt = con.createStatement();

stmt.executeUpdate(dbQuery);

count = 1;

}

catch(Exception e){

count = -1;

}

return count;

}

}

[5518 byte] By [peixinga] at [2007-11-27 11:26:46]
# 1

Well, you don't. You only need to call that executeUpdate() once.

Of course you subsequently need to either commit the update or close the connection to get the insert finalized. But you don't do either of them so it gets lost.

DrClapa at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 2

DrClap, are you able to give me a solution as to how can i commit the update or close the connection ? however i notice something you might have "mistakened"

its not that i can't create a new record into my database... when i only put a single executeUpdate(), the new record is not created, but when i put 2 lines of executeUpdate() then the new record is reflected in the database...

hope you may give me some code samples to clarify my doubts to this issue.

thanks in advance

peixing.

peixinga at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 3

> DrClap, are you able to give me a solution as to how

> can i commit the update or close the connection ?

> however i notice something you might have

> "mistakened"

If you are using MS Access then you have to do one of the following for a modification of the database to show up

- close the connection

- use a transaction

- do another sql statement of any type including a query.

jschella at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 4

By the way you need to close your resources when you are done with them. Resources are result sets, statements and connections.

jschella at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 5

jschell, thanks for your advice... thought i have yet to try your methods, may i know what's a "transaction" you mentioned above? and i got 1 request... if you may, i hope you can bookmark this page as i might need your help in future ;)

thanks

Peixing.

peixinga at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 6

> jschell, thanks for your advice... thought i have yet

> to try your methods, may i know what's a

> "transaction" you mentioned above?

Common functionality supplied by databases - read up on them to learn more.

jschella at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...
# 7

jschell, thanks for all the help and to others who helped out too !!!

i have managed to solve this issue by closing my "statement" as what you have told me to.

really thanks to you so much!!

Peixing.

peixinga at 2007-7-29 16:12:26 > top of Java-index,Java Essentials,Java Programming...