日期:2014-05-20 浏览次数:20798 次
import java.sql.*; public class Main { public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test?user=root&password=jiakai"); String sql = "select * from book where id = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, 5); ResultSet rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("id: "+id+" name: "+name); } } }
------解决方案--------------------
动态的拼接SQL,参数用"?"代替,可以PrepareStatement里面的方法给"?"赋值,得到的ResultSet结果集,再从结果集里去数据,建议不用"*",只查询你需要的字段。
------解决方案--------------------