Java操作Oracle并实现增删改查

Posted 海乐学习-专业呼叫中心解决方案

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java操作Oracle并实现增删改查相关的知识,希望对你有一定的参考价值。

由于Oracle授权问题,Maven3不提供Oracle JDBC driver,需要手动添加 (具体方法在上面的随笔中有详细说明)

直接上代码

OracleUtil 连接类

package com.JavaOracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleUtil 
    //数据库连接地址
    private static String url="jdbc:oracle:thin:@192.168.1.180:1521:orcl";
    //用户名
    private static String username= "sys as sysdba";
    //密码
    private static String password ="admindljjk";
    //驱动名称
    private static String jdbcName = "oracle.jdbc.OracleDriver";

    /*获取数据库连接 */
    public static Connection getCon()
        try 
            Class.forName(jdbcName);
         catch (ClassNotFoundException e) 
            e.printStackTrace();
        
        Connection con = null;
        try 
            con = DriverManager.getConnection(url,username,password);
         catch (SQLException e) 
            e.printStackTrace();
        
        return con;
    

    /*关闭数据库连接*/
    public static void closeCon(Connection con) throws SQLException 
        if (con != null)
            con.close();
    

    /*
    public static void main(String[] args)
        try 
            getCon();
            System.out.println("数据库连接成功");
         catch (Exception e) 
            e.printStackTrace();
            System.out.println("数据库连接失败");
        
    
    */

OracleDemo   具体业务增删改查类

package com.JavaOracle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OracleDemo 

    //连接对象
    Connection connection=null;
    //创建预编译对象
    PreparedStatement ps=null;
    //创建结果集
    ResultSet rs = null;

    /*插入*/
    public int insert()
        int result = 0;
        connection = OracleUtil.getCon();
        String sql = "insert into CTI_AGENT_WORK_REST ";
        sql=sql+" (ID,AGENT_NAME,DATE_TIME,STATUS) ";
        sql=sql+" values(?,?,?,? )";
        try 
            ps = connection.prepareStatement(sql);
            ps.setInt(1,2323232);
            ps.setString(2,"老刘");
            ps.setString(3,"2021-07-26 15:51:12");
            ps.setString(4,"Rest");

            result = ps.executeUpdate();
         catch (SQLException e) 
            e.printStackTrace();
        finally 
            try 
                OracleUtil.closeCon(connection);
            catch (SQLException e) 
                e.printStackTrace();
            
        
        return result;
    
    /*查询*/
    public void select()
        connection = OracleUtil.getCon();
        String sql = "select * from CTI_AGENT_WORK_REST";
        try 
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next())
                int id = rs.getInt(1);
                String AGENT_NAME = rs.getString(2);
                String DATE_TIME = rs.getString(3);
                String STATUS= rs.getString(4);
                System.out.println("ID:"+id + " AGENT_NAME:"+AGENT_NAME+" DATE_TIME:"+DATE_TIME+" STATUS:"+STATUS);
            
         catch (SQLException e) 
            e.printStackTrace();
        finally 
            try 
                OracleUtil.closeCon(connection);
            catch (SQLException e) 
                e.printStackTrace();
            
        
    
    /*修改*/
    public int update()
        connection = OracleUtil.getCon();
        String sql = "update student set name = ?,gender = ? where id = ?";
        int result = 0;
        try 
            ps = connection.prepareStatement(sql);
            ps.setString(1,"老张");
            ps.setString(2,"男");
            ps.setInt(3,2);
            result = ps.executeUpdate();
         catch (SQLException e) 
            e.printStackTrace();
        finally 
            try 
                OracleUtil.closeCon(connection);
            catch (SQLException e) 
                e.printStackTrace();
            
        

        return result;

    

    /*删除 */
    public int delete()
        int result = 0;
        connection = OracleUtil.getCon();
        String sql = "delete from student where id = ?";
        try 
            ps = connection.prepareStatement(sql);
            ps.setInt(1,1);
            result = ps.executeUpdate();
         catch (SQLException e) 
            e.printStackTrace();
        finally 
            try 
                OracleUtil.closeCon(connection);
            catch (SQLException e) 
                e.printStackTrace();
            
        
        return result;
    

    /*
    public static void main(String[] args)
        OracleDemo od = new OracleDemo();
        //int add = od.add();
        System.out.println(add);
        od.select();
//        System.out.println(od.delete());
    */

主函数
public static void main( String[] args )
 
        System.out.println( "Hello JavaOracle !" );

        OracleDemo myDB = new OracleDemo();
        int result = myDB.insert();
        System.out.println("insert 结果: "+result);
        myDB.select();
        //System.out.println(myDB.delete());
 

 


以上是关于Java操作Oracle并实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章

java连接sql数据库,怎么实现增删改查?

oracle表的crud(增删改查)单表

java jdbc连接数据库 oracle的代码实现增删改查的方法怎么写

学习下oracle的存储过程增删改查操作以及java调用存储过程

用java做界面连接oracle数据库实现增删改查功能,具体步骤怎么做?

java连接Oracle数据库实现增删改查并在Navicat中显示