JAVA-JDBC连接增编削查

金歌  金牌会员 | 2025-2-21 04:29:22 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 916|帖子 916|积分 2748

 起首与oracle创建连接
  1. package com.ma;
  2. import com.sun.media.sound.SoftTuning;
  3. import java.sql.*;
  4. public class JDBCtest {
  5.     public static void main(String[] args) throws Exception {
  6.         //1.加载驱动
  7.         Class.forName("oracle.jdbc.driver.OracleDriver");
  8.         //2.建立连接
  9.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
  10.         //3.测试建立连接
  11.         System.out.println(connection);
  12.         //4.定义sql语句
  13.         String sql ="select * from emp";
  14.         //5.准备静态处理块对象,将sql语句放置到静态代码块中
  15.         Statement statement=connection.createStatement();
  16.         //6.执行sql语句,返回值对象是结果集合
  17.         ResultSet resultSet = statement.executeQuery(sql);
  18.         //7.循环处理
  19.         while (resultSet.next()){
  20.             int anInt = resultSet.getInt(1);
  21.             System.out.println(anInt);
  22.             String ename = resultSet.getString("ename");
  23.             System.out.println(ename);
  24.             System.out.println("----------------");
  25.         }
  26.         //8.关闭连接
  27.         statement.close();
  28.         connection.close();
  29.     }
  30. }
复制代码
 测试连接
  1. package com.ma;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.Statement;
  5. import java.util.function.Consumer;
  6. public class CreateTable {
  7.     public static void main(String[] args) throws Exception {
  8.         Class.forName("oracle.jdbc.driver.OracleDriver");
  9.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
  10.         Statement statement = connection.createStatement();
  11.         String sql="create table psn(id number(10) primary key,name varchar2(10))";
  12.         boolean execute = statement.execute(sql);
  13.         System.out.println(execute);
  14.         statement.close();
  15.         connection.close();
  16.     }
  17. }
复制代码
减少代码冗余性,创建代码块
  1. package com.ma.util;
  2. import java.sql.*;
  3. public class DBUtil {
  4.     public static final  String URL="jdbc:oracle:thin:@localhost:1521:orcl";
  5.     public static final  String USENAME="scott";
  6.     public static final  String PASSWORE="123456";
  7.     static {
  8.         try {
  9.             Class.forName("oracle.jdbc.driver.OracleDriver");
  10.         } catch (ClassNotFoundException e) {
  11.             throw new RuntimeException(e);
  12.         }
  13.     }
  14.     public static Connection getConnection(){
  15.         try {
  16.             return DriverManager.getConnection(URL,USENAME,PASSWORE);
  17.         } catch (SQLException e) {
  18.             throw new RuntimeException(e);
  19.         }
  20.     }
  21.     public static  void closeConnection(Connection connection, Statement statement) {
  22.        if(statement!=null){
  23.            try {
  24.                statement.close();
  25.            } catch (SQLException e) {
  26.                throw new RuntimeException(e);
  27.            }
  28.        }
  29.         if (connection != null) {
  30.             try {
  31.                 connection.close();
  32.             } catch (SQLException e) {
  33.                 throw new RuntimeException(e);
  34.             }
  35.         }
  36.     }
  37.     public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
  38.        if(resultSet!=null){
  39.            try {
  40.                resultSet.close();
  41.            } catch (SQLException e) {
  42.                throw new RuntimeException(e);
  43.            }
  44.        }
  45.         if(statement!=null){
  46.             try {
  47.                 statement.close();
  48.             } catch (SQLException e) {
  49.                 throw new RuntimeException(e);
  50.             }
  51.         }
  52.         if (connection != null) {
  53.             try {
  54.                 connection.close();
  55.             } catch (SQLException e) {
  56.                 throw new RuntimeException(e);
  57.             }
  58.         }
  59.     }
  60. }
复制代码
下面创建增编削查
  1. package com.ma.dao;
  2. import com.ma.entity.Emp;
  3. public interface EmpDao {
  4.     public void insert(Emp emp);
  5.     public void delete(Emp emp);
  6.     public void update(Emp emp);
  7.     public Emp getEmpByEmpno(Integer empno);
  8. }
复制代码
  1. package com.ma.dao.impl;
  2. import com.ma.dao.EmpDao;
  3. import com.ma.entity.Emp;
  4. import com.ma.util.DBUtil;
  5. import java.security.PublicKey;
  6. import java.sql.Connection;
  7. import java.sql.Date;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. public class EmpDaolmpl implements EmpDao {
  11.     @Override
  12.     public void insert(Emp emp) {
  13.         Connection connection=null;
  14.         Statement statement=null;
  15.         try {
  16.             connection= DBUtil.getConnection();
  17.             statement=connection.createStatement();
  18.             String sql="insert into emp values("+emp.getEmpno()+",'"+emp.getEname()+"','"+emp.getJob()+"'," +
  19.                     ""+emp.getMrg()+",to_date('"+emp.getHiredate()+"','YYYY-MM-DD'),"+emp.getSal()+"," +
  20.                     ""+emp.getComm()+","+emp.getDeptno()+")";
  21.             System.out.println(sql);
  22.             int i = statement.executeUpdate(sql);
  23.             System.out.println("受影响的行数是:"+i);
  24.         } catch (SQLException e) {
  25.             throw new RuntimeException(e);
  26.         }finally {
  27.             DBUtil.closeConnection(connection,statement);
  28.         }
  29.     }
  30.     @Override
  31.     public void delete(Emp emp) {
  32.         Connection connection=null;
  33.         Statement statement=null;
  34.         try {
  35.             connection= DBUtil.getConnection();
  36.             statement=connection.createStatement();
  37.             String sql="delete from emp where empno="+emp.getEmpno();
  38.             System.out.println(sql);
  39.             int i = statement.executeUpdate(sql);
  40.             System.out.println("受影响的行数是:"+i);
  41.         } catch (SQLException e) {
  42.             throw new RuntimeException(e);
  43.         }finally {
  44.             DBUtil.closeConnection(connection,statement);
  45.         }
  46.     }
  47.     @Override
  48.     public void update(Emp emp) {
  49.         Connection connection=null;
  50.         Statement statement=null;
  51.         try {
  52.             connection= DBUtil.getConnection();
  53.             statement=connection.createStatement();
  54.             String sql="update emp set job='"+emp.getJob()+"'where empno ="+emp.getEmpno();
  55.             System.out.println(sql);
  56.             int i = statement.executeUpdate(sql);
  57.             System.out.println("受影响的行数是:"+i);
  58.         } catch (SQLException e) {
  59.             throw new RuntimeException(e);
  60.         }finally {
  61.             DBUtil.closeConnection(connection,statement);
  62.         }
  63.     }
  64.     @Override
  65.     public Emp getEmpByEmpno(Integer empno) {
  66.         return null;
  67.     }
  68.     public static void main(String[] args) {
  69.         EmpDao empDao=new EmpDaolmpl();
  70.         Emp emp=new Emp(222,"sisi","SALES",1111,"2025-2-18",1500.0,500.0,10);
  71. //        empDao.insert(emp);
  72. //        empDao.delete(emp);
  73.         empDao.update(emp);
  74.     }
  75. }
复制代码
  1. package com.ma.entity;
  2. import java.util.Date;
  3. public class Emp {
  4.     private Integer empno;
  5.     private String ename;
  6.     private String job;
  7.     private Integer mrg;
  8.     private String hiredate;
  9.     private Double sal;
  10.     private Double comm;
  11.     private  Integer deptno;
  12.     public  Emp(){
  13.     }
  14.     public Emp(Integer empno, String ename, String job, Integer mrg, String hiredate, Double sal, Double comm, Integer deptno) {
  15.         this.empno = empno;
  16.         this.ename = ename;
  17.         this.job = job;
  18.         this.mrg = mrg;
  19.         this.hiredate = hiredate;
  20.         this.sal = sal;
  21.         this.comm = comm;
  22.         this.deptno = deptno;
  23.     }
  24.     public Integer getEmpno() {
  25.         return empno;
  26.     }
  27.     public void setEmpno(Integer empno) {
  28.         this.empno = empno;
  29.     }
  30.     public String getEname() {
  31.         return ename;
  32.     }
  33.     public void setEname(String ename) {
  34.         this.ename = ename;
  35.     }
  36.     public String getJob() {
  37.         return job;
  38.     }
  39.     public void setJob(String job) {
  40.         this.job = job;
  41.     }
  42.     public Integer getMrg() {
  43.         return mrg;
  44.     }
  45.     public void setMrg(Integer mrg) {
  46.         this.mrg = mrg;
  47.     }
  48.     public String getHiredate() {
  49.         return hiredate;
  50.     }
  51.     public void setHiredate(String hiredate) {
  52.         this.hiredate = hiredate;
  53.     }
  54.     public Double getSal() {
  55.         return sal;
  56.     }
  57.     public void setSal(Double sal) {
  58.         this.sal = sal;
  59.     }
  60.     public Double getComm() {
  61.         return comm;
  62.     }
  63.     public void setComm(Double comm) {
  64.         this.comm = comm;
  65.     }
  66.     public Integer getDeptno() {
  67.         return deptno;
  68.     }
  69.     public void setDeptno(Integer deptno) {
  70.         this.deptno = deptno;
  71.     }
  72.     @Override
  73.     public String toString() {
  74.         return "Emp{" +
  75.                 "empno=" + empno +
  76.                 ", ename='" + ename + '\'' +
  77.                 ", job='" + job + '\'' +
  78.                 ", mrg=" + mrg +
  79.                 ", hiredate=" + hiredate +
  80.                 ", sal=" + sal +
  81.                 ", comm=" + comm +
  82.                 ", deptno=" + deptno +
  83.                 '}';
  84.     }
  85. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表