package org.zp.preparedStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.zp.util.DbUtil;
import org.zp.entity.Student;
public class preparedStatement {
public static void main(String[] args) {
String sql = "select * from T_Student where stu_sex =? and stu_age<? and stu_age>?";
Student student = null;
Connection conn = DbUtil.getConnection();
PreparedStatement pstmt = null;
ResultSet rs=null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "男");
pstmt.setString(2, "24");
pstmt.setString(3, "20");
rs=pstmt.executeQuery();
while(rs.next()) {
student = new Student(rs.getInt("stu_id"),
rs.getString("stu_Name"), rs.getString("stu_Age"),
rs.getString("stu_sex"), rs.getString("stu_Dept"),
rs.getString("stu_Class"), rs.getString("stu_Birth"),
rs.getString("notes"));
System.out.println(student.getStu_name()+", "+student.getStu_age());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}