我想将字符串作为参数传递给我的JDBC程序。该代码适用于INT类型输入,但会在字符串类型输入上捕获块。
我想修改程序,使其可以随机捕获所有类型的输入。因为,我需要运行以下 java prog。在 bash 脚本中。所以一旦生成了类文件,我应该为程序提供随机输入并获得输出......
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 !!");
}
}
}
对于int var = 123050044,输出为
Connected?
student
对于String var = "sir",输出为
Connected?
wrong !!