1.熟悉快捷键生成setter/getter、toString、constructor
	2.resultset转对象
master
丁业林-17050417 3 years ago
parent 1007ca363a
commit 01a05df1fa

@ -0,0 +1,107 @@
package com.msb.bean;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
public Emp() {
}
public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm, Integer deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}

@ -1,6 +1,10 @@
package com.msb.test1;
import com.msb.bean.Emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TestJDBC5 {
public static String driver = "com.mysql.cj.jdbc.Driver";
@ -10,10 +14,15 @@ public class TestJDBC5 {
public static void main(String[] args) {
testQuery();
List<Emp> list = testQuery();
for (Emp emp : list) {
System.out.println(emp);
}
}
public static void testQuery() {
public static List<Emp> testQuery() {
List<Emp> result = null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
@ -31,6 +40,7 @@ public class TestJDBC5 {
statement = connection.createStatement();
String sql = "select * from emp;";
resultSet = statement.executeQuery(sql);
result = new ArrayList<Emp>();
while (resultSet.next()) {
int empno = resultSet.getInt("empno");
String ename = resultSet.getString("ename");
@ -40,11 +50,14 @@ public class TestJDBC5 {
double sal = resultSet.getDouble("sal");
double comm = resultSet.getDouble("comm");
int deptno = resultSet.getInt("deptno");
System.out.println("" + empno + "\t" + ename + "\t" + job + "\t" + mgr + "\t" + hiredate + "\t" + sal + "\t" + comm + "\t" + deptno + "\t");
Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
result.add(emp);
}
}
} catch (Exception e) {
catch (Exception e) {
e.printStackTrace();
} finally {
}
finally {
try {
if (resultSet != null) {
resultSet.close();
@ -55,9 +68,12 @@ public class TestJDBC5 {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
}
catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}

Loading…
Cancel
Save