这个问题主要是匹配好引号和单引号即可。
如果是varchar型,那么变量要带单引号('),如果是int型就不用带。同时要注意用+号进行String的拼接。
示例:
public List<Student> huizong_bujige(String kemu1){
List<Student> list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "select student.num,name,banji,kemu,chengji from student join sutdent_chengji sc on student.num = sc.num and chengji<60 and kemu='"+kemu1+"'"";
//在这里,因为kemu为varchar类型,所以要拿单引号引起来。表面看起来这里是差一个双引号,
但是真正的sql语句是不带双引号的,所以不用与第一个双引号配对,也就是说最后面没有红色的双引号与第一个双引号对应,因为第一个双引号本身就是为了满足Java的语法,真正的SQL语句不带双引号。
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {//这里要注意用列名来确定。
String num=resultSet.getString("num");
String name=resultSet.getString("name");
String banji=resultSet.getString("banji");
kemu1=resultSet.getString("kemu");
String chengji=resultSet.getString("chengji");
Student student=new Student(num,name,banji,kemu1,chengji);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, resultSet);
}
return list;
}
多个参数示例:
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
标签:多表,String,getString,双引号,chengji,resultSet,num,SQL,Javaweb From: https://www.cnblogs.com/joranger/p/17441579.html