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.

NEVER NEVER NEVER ignore an exception when writing or debugging code!
Java has created an instance of SQLException packed full of debugging info for you, and you have chosen to ignore it. Bad move.
Repeat after me: e.printStackTrace() e.printStackTrace() e.printStackTrace() ...

:)
J

commented: that was fun to read! :D and it worked :D +3
commented: Step 0 should be this +6

ohh yeah!! i forgot that... about a week away from java .. and its having its effects! let me try that out!

done! printStackTrace() pointed this out
Can not issue data manipulation statements with executeQuery().
and thats what i was doing wrong.. changed to executeUpdate() and peace is restored! :)
thanks :) problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.