从中获取字符串名称变量JTextField我正在尝试从数据库中匹配该列值并获取该列的所有行值public User1 getUserDestination(String name){ try{ //String s="Select * FROM trainschedule WHERE Destination="+name1; rs=stm.executeQuery("SELECT * FROM trainschedule WHERE Destination="+name); // rs=stm.executeQuery(s); if(rs.next()){ user1=new User1(); user1.setTrainNo(Integer.parseInt(rs.getString("Train_No"))); user1.setTrainName((rs.getString("tName"))); user1.setDeparture((rs.getString("Departure"))); user1.setDepartureTime((rs.getString("dTime"))); user1.setArrival((rs.getString("Arrival"))); user1.setArrivalTime((rs.getString("aTime"))); user1.setTicketPrice((rs.getString("Ticket_Price"))); System.out.println(user1.getTrainName()); return user1; } else{ return null; } }catch(Exception e){ e.printStackTrace(); } return null;}总是给我这样的sql异常Unknown column in 'where clause' at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97).
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
如评论中所述,您应该这样做(使用 PreparedStatement)
PreparedStatement ps = connection.prepareStatement("SELECT * FROM trainschedule WHERE Destination = ?");
ps.setString(1, name);
rs = ps.executeQuery();
if(rs.next()){
...
添加回答
举报
0/150
提交
取消