用预编译对象
PreparedStatement
例:
PreparedStatement pstmt = new PreparedStatement();
public static String select(int sno)
{
String sql = "select * from student where sno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, sno);
rs = pstmt.executeQuery();
while(rs.next())
{
message = "学号"+rs.getString("sno")+" 姓名"+rs.getString("sname")
}
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
DBDao.close(rs, pstmt, conn);
}
return message;
}
使用动态变量就使用准备语句喽PrepareStatement,当然你也可以每次执行都直接字符串操作替换sql语句中的问号:
准备语句就是这种喽
try{
PreparedStatement pstmt = Connection.getPreparedStatement(sql);
pstmt.setInt(begin,1);
pstmt.setInt(end,2);
ResultSet rs = pstmt.executeQuery();
}catch(SQLException e) finally{}
Connection conn = getConnection();
PreparedStatement pstmt = conn.getPreparedStatement(sql);
ResultSet rs = null;
pstmt.setInt(begin,1);
pstmt.setInt(end,2);
rs = pstmt.execute();
另:limit 第一个参数是开始行数,第二个参数是选多少条,而不是END。