zoukankan      html  css  js  c++  java
  • java 调用 MySQL 的存储过程 和链接MySQL数据库查询

      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());}}

  • 相关阅读:
    Oracle数据库链Database links
    记录中文字符的烦恼
    oracle游标应用难点 sys_refcursor 和 cursor(转)
    C# Delegate类
    Oracle_merge into 中 using 后的查询表如果有参数的情况
    C# partial
    Excel快捷键
    时间格式的问题
    Ref_cursor
    .Net 引用命名空间
  • 原文地址:https://www.cnblogs.com/Lvhengshuai/p/6816432.html
Copyright © 2011-2022 走看看