double execution to insert into database ?!? help me someone
Sorry if i have repost something similar to my previous post... but somehow i think i really need a major help in this. I need to write 2 lines of executeUpdate() then i am able to insert a new record into my database...i figure for so long and still couldnt solve this bug in my application. Hope someone can offer me a solution to me... thanks in advance.
Peixing.
import java.io.*;
public class Index{
public static void main(String[] args)throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String userId = "";
String userPw = "";
String email = "";
System.out.println("Welcome to New World Inc.");
System.out.println("1. Create new user");
System.out.println("1. Login application");
System.out.println("3. Exit application");
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();
int idSize = userId.length();
}
catch(Exception e){
e.printStackTrace();
}
}while(userId.equals(""));
}
}
}
import java.sql.*;
public class Account{
DBC dbc;
public Account(){
dbc = new DBC();
dbc.setUp("database");
}
public void 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.*;
public class DBC{
private Connection con;
public void 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();
}
}
public ResultSet readRequest(String dbQuery){
ResultSet rs=null;
try{
Statement stmt = con.createStatement();
rs = stmt.executeQuery(dbQuery);
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
public int updateRequest(String dbQuery){
int count=0;
try{
Statement stmt = con.createStatement();
stmt.executeUpdate(dbQuery);
count = 1;
}
catch(Exception e){
count = -1;
}
return count;
}
}

