1 package com.function; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.time.Period; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.MySQL.jdbc; 11 import com.beng.emp; 12 13 public class oufunction { 14 //连接数据库的方法 15 jdbc j=new jdbc(); 16 17 18 //用 MySQL数据库 查询数据 19 20 21 public List<emp> selecEMP(){ 22 List<emp> list=new ArrayList<>(); 23 Connection conn=null; 24 PreparedStatement ps=null; 25 ResultSet rs=null; 26 try { 27 //连接数据库 28 conn=j.getjdbc(); 29 String sql=" select * from emp"; 30 ps=conn.prepareStatement(sql); 31 rs=ps.executeQuery(); 32 while(rs!=null && rs.next()){ 33 emp e=new emp(); 34 e.setEid(rs.getString(1)); 35 e.setEname(rs.getString(2)); 36 e.setSex(rs.getString(3)); 37 e.setHire(rs.getString(4)); 38 e.setSar(rs.getString(5)); 39 e.setDid(rs.getString(6)); 40 list.add(e); 41 } 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 return list; 46 } 47 48 49 50 //调用 MySQL 数据库中的存储过程(根据学生编号查询学生信息) 51 public emp selecEMP(String eid){ 52 emp em=null; 53 Connection conn=null; 54 PreparedStatement ps=null; 55 ResultSet rs=null; 56 57 try { 58 //连接数据库 59 conn=j.getjdbc(); 60 String sql="{call proce1(?)}"; 61 ps=conn.prepareStatement(sql); 62 ps.setString(1, eid); 63 rs=ps.executeQuery(); 64 while(rs!=null && rs.next()){ 65 em=new emp(); 66 em.setEid(rs.getString(1)); 67 em.setEname(rs.getString(2)); 68 em.setSex(rs.getString(3)); 69 em.setHire(rs.getString(4)); 70 em.setSex(rs.getString(5)); 71 em.setDid(rs.getString(6)); 72 } 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 return em; 77 } 78 79 80 81 82 83 84 //测试 85 public static void main(String[] args) { 86 oufunction fun=new oufunction(); 87 88 89 List<emp> list=fun.selecEMP(); 90 91 for (emp s : list) { 92 System.out.println("员工姓名是:"+s.getEname()); 93 } 94 95 emp e=fun.selecEMP("2"); 96 System.out.println(e.getEname()); 97 98 99 100 101 } 102 103 104 105 106 107 }
package com.function;
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.time.Period;import java.util.ArrayList;import java.util.List;
import com.MySQL.jdbc;import com.beng.emp;
public class oufunction {//连接数据库的方法jdbc j=new jdbc();
//用 MySQL数据库 查询数据
public List<emp> selecEMP(){List<emp> list=new ArrayList<>();Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {//连接数据库conn=j.getjdbc();String sql=" select * from emp";ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs!=null && rs.next()){emp e=new emp();e.setEid(rs.getString(1));e.setEname(rs.getString(2));e.setSex(rs.getString(3));e.setHire(rs.getString(4));e.setSar(rs.getString(5));e.setDid(rs.getString(6));list.add(e);}} catch (Exception e) {e.printStackTrace();}return list;}//调用 MySQL 数据库中的存储过程(根据学生编号查询学生信息)public emp selecEMP(String eid){emp em=null;Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {//连接数据库conn=j.getjdbc();String sql="{call proce1(?)}";ps=conn.prepareStatement(sql);ps.setString(1, eid);rs=ps.executeQuery();while(rs!=null && rs.next()){em=new emp();em.setEid(rs.getString(1));em.setEname(rs.getString(2));em.setSex(rs.getString(3));em.setHire(rs.getString(4));em.setSex(rs.getString(5));em.setDid(rs.getString(6));}} catch (Exception e) {e.printStackTrace();}return em;}//测试public static void main(String[] args) {oufunction fun=new oufunction();List<emp> list=fun.selecEMP();for (emp s : list) {System.out.println("员工姓名是:"+s.getEname());}emp e=fun.selecEMP("2");System.out.println(e.getEname());}}