I want to pass strings as parameters to my JDBC program. The code works fine for INT type inputs but goes to catch block on string type inputs.
I wanted to modify the program such that it can capture all types of inputs at random. Because, I need to run the following java prog. in bash script. So once class file is generated, i supposed to provide random inputs to the program and obtain the output...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class JDBC_run {
public static void main(String[] args) throws Exception {
Connection conn = null;
// int var = 123050044 ;
String var = "sir";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&password=root");
ResultSet rs;
System.out.println("Connected?");
try{
Statement st = (com.mysql.jdbc.Statement) conn.createStatement();
rs = st.executeQuery("SELECT type FROM Table_new WHERE id = " + var);
while ( rs.next() ) {
String lastName = rs.getString("type");
System.out.println(lastName);
}
}
catch(SQLException s){
System.out.println("wrong !!");
}
}
}
For int var = 123050044, the output is
Connected?
student
For String var = "sir", the output is
Connected?
wrong !!