this is the problem code :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
public class Prepared {
public static void main(String[] args) {
try {
readerHelper r = readerHelper.GET_INSTANCE;
System.out.print("enter username: ");
String user = r.is().readLine();
System.out.print("enter password: ");
String pass = r.is().readLine();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb",user,pass);
PreparedStatement selAll = con.prepareStatement("INSERT INTO Authors(name) VALUES(?)");
selAll.setString(1, "dan brown");
}catch(IOException e){
System.out.println("error reading input");
}catch(SQLException e){
System.out.println("error finding source");
}
}
}
this gives me "error finding source" after i put in the username and password. yet , on a nearly similar code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
public class Version {
public static void main(String[] args){
try{
readerHelper r = readerHelper.GET_INSTANCE;
System.out.print("enter username: ");
String user = r.is().readLine();
System.out.print("enter password: ");
String pass = r.is().readLine();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb",user,pass);
PreparedStatement selAll = con.prepareStatement("select version()");
ResultSet result = selAll.executeQuery();
while(result.next()){
System.out.println(result.getString(1));
}
}catch(SQLException e){
System.out.println("error finding source");
}catch(IOException e){
System.out.println("error reading input");
}
}
}
im not getting any errors , and its displaying my MySQL version correctly.
what am i doing worng in my first code? the line where the connection is made is same in both codes , yet one works , and the other doesnt...
lastly, my reader class used in the above two codes :
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class readerHelper {
public static readerHelper GET_INSTANCE = new readerHelper();
private BufferedReader is;
private readerHelper() {
is = new BufferedReader(new InputStreamReader(System.in));
}
public BufferedReader is() {
return is;
}
}
some help in figuring out the problem would be great... :)
thanks and regards
somjit.