package cn.lijun.entity; public class Student1 {
private int id;
private String sname;
private int gender;
private String brithday; @Override
public String toString() {
return "Student1{" +
"id=" + id +
", sname='" + sname + '\'' +
", gender=" + gender +
", brithday='" + brithday + '\'' +
'}';
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public int getGender() {
return gender;
} public void setGender(int gender) {
this.gender = gender;
} public String getBrithday() {
return brithday;
} public void setBrithday(String brithday) {
this.brithday = brithday;
}
} package cn.linjun.demo;
/*
* 使用PreparedStatement 查询一条数据 封装成一个学生的Student1对象
* **/ import JdbcUtils.JdbcDemo2;
import JdbcUtils.JdbcUtils;
import cn.lijun.entity.Student1; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class Demo3Student {
public static void main(String[] args) throws SQLException {
//创建学生对象
Student1 s=new Student1();
Connection conn = JdbcUtils.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from student1 where id=?");
ps.setInt(1,1);
ResultSet rs = ps.executeQuery();
if(rs.next()){
//封装成一个学生对象
s.setId(rs.getInt("id"));
s.setSname(rs.getString("sname"));
s.setGender(rs.getInt("gender"));
s.setBrithday(rs.getString("brithday")); }
JdbcUtils.close(rs,ps,conn);
System.out.println(s); } }