package JDBC;
import java. util. Date;
public class resultset {
private int id;
private String ename;
private int job_id;
private int mgr;
private Date joindate;
private float salary;
private float bonus;
private int dept_id;
public resultset ( ) {
}
public resultset ( int id, String ename, int job_id, int mgr, Date joindate, float salary, float bonus, int dept_id) {
this . id = id;
this . ename = ename;
this . job_id = job_id;
this . mgr = mgr;
this . joindate = joindate;
this . salary = salary;
this . bonus = bonus;
this . dept_id = dept_id;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getEname ( ) {
return ename;
}
public void setEname ( String ename) {
this . ename = ename;
}
public int getJob_id ( ) {
return job_id;
}
public void setJob_id ( int job_id) {
this . job_id = job_id;
}
public int getMgr ( ) {
return mgr;
}
public void setMgr ( int mgr) {
this . mgr = mgr;
}
public Date getJoindate ( ) {
return joindate;
}
public void setJoindate ( Date joindate) {
this . joindate = joindate;
}
public float getSalary ( ) {
return salary;
}
public void setSalary ( float salary) {
this . salary = salary;
}
public float getBonus ( ) {
return bonus;
}
public void setBonus ( float bonus) {
this . bonus = bonus;
}
public int getDept_id ( ) {
return dept_id;
}
public void setDept_id ( int dept_id) {
this . dept_id = dept_id;
}
@Override
public String toString ( ) {
return "resultset{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bonus=" + bonus +
", dept_id=" + dept_id +
'}' ;
}
}
package JDBC;
import java. sql. *;
import java. util. ArrayList;
import java. util. HashSet;
import java. util. List;
import java. util. Set;
public class Demo01 {
public static void main ( String[ ] args) {
Connection conn = null;
Statement stmt = null;
Set< resultset> setlist = new HashSet < > ( ) ;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
conn = DriverManager. getConnection ( "jdbc:mysql:///db05" , "root" , "root" ) ;
stmt = conn. createStatement ( ) ;
String sql = "select * from emp" ;
ResultSet res = stmt. executeQuery ( sql) ;
while ( res. next ( ) ) {
resultset resultset = new resultset ( ) ;
resultset. setId ( res. getInt ( "id" ) ) ;
resultset. setEname ( res. getString ( "ename" ) ) ;
resultset. setJob_id ( res. getInt ( "job_id" ) ) ;
resultset. setMgr ( res. getInt ( "mgr" ) ) ;
resultset. setJoindate ( res. getDate ( "joindate" ) ) ;
resultset. setSalary ( res. getFloat ( "salary" ) ) ;
resultset. setBonus ( res. getFloat ( "bonus" ) ) ;
resultset. setDept_id ( res. getInt ( "dept_id" ) ) ;
setlist. add ( resultset) ;
}
setlist. stream ( ) . forEach ( i - > System. out. println ( i) ) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
if ( stmt != null) {
try {
stmt. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
if ( conn != null) {
try {
conn. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
}
}