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;
}
}

