Jdbc增删改查的相关操作(Oracle 数据库环境)

验证java IDE是否正确连接Oracle

导入包

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

/**
 * 数据库连接测试
 * 连上给个显示下面  ojdbc6-g.jar
 驱动程序已装载,即将连接数据库。
 ------------------------------------------------
 数据库版本:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 驱动程序名称:Oracle JDBC driver
 驱动程序版本:11.2.0.1.0
 ------------------------------------------------
 */

public class Oracle_Connect_test {
    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("驱动程序已装载,即将连接数据库。");
        } catch (ClassNotFoundException ex) {
            System.out.println("无法加载驱动程序:" + ex.getMessage());
            return;
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.204.154:1521:orcl",
                    "scott", "123");
            DatabaseMetaData md = conn.getMetaData();
            System.out.println("------------------------------------------------");
            System.out.print("数据库版本:");
            System.out.println(md.getDatabaseProductVersion());
            System.out.print("驱动程序名称:");
            System.out.println(md.getDriverName());
            System.out.print("驱动程序版本:");
            System.out.println(md.getDriverVersion());
            System.out.println("------------------------------------------------");
            conn.close();
        } catch (SQLException ex) {
            System.out.println("数据库连接失败:" + ex.getMessage());
            ex.printStackTrace();
            return;
        }
    }
        }

建表及其数据

CREATE TABLE "SCOTT"."用户表" (
  "用户ID" VARCHAR2(20) NOT NULL ,
  "用户名" VARCHAR2(20) ,
  "密码" VARCHAR2(30) ,
  "Email" VARCHAR2(30) ,
  "用户类型" VARCHAR2(30) ,
  "注册日期" DATE DEFAULT sysdate ,
  PRIMARY KEY ("用户ID"),
  CONSTRAINT "用户名" UNIQUE ("用户名")
)

INSERT INTO "用户表"("用户ID", "用户名", "密码", "EMAIL", "用户类型", "注册日期") VALUES ('111122', '尹xx', '9988111', 'zhenqcom', '5', TO_DATE('2020-02-12 20:39:58', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "用户表"("用户ID", "用户名", "密码", "EMAIL", "用户类型", "注册日期") VALUES ('8207919', 'Tony 哥', '6688', 'zhenqk@163.com', '5', TO_DATE('2020-02-12 20:49:04', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "用户表"("用户ID", "用户名", "密码", "EMAIL", "用户类型", "注册日期") VALUES ('22222', 'Tony ', '6688', '610@com', '5', TO_DATE('2020-02-12 20:49:04', 'SYYYY-MM-DD HH24:MI:SS'));

java程序--增加 修改 查询

import javax.swing.*;
import java.sql.*;
class GetDataClass{
//数据库  获取连接
    public Connection getOracleConn() {
        Connection conn = null;
        String driver = "oracle.jdbc.driver.OracleDriver";
        String connectURL = "jdbc:oracle:thin:@192.168.204.154:1521:orcl";
        String loginName = "scott";
        String loginPassword = "123";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "无法加载驱动程序:" + ex.getMessage());
        }
        try {
            conn = DriverManager.getConnection(connectURL, loginName, loginPassword);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return conn;
    }


    public void closeConnection(Connection conn) throws SQLException {
        conn.close();
    }

    public void closeResultSet(ResultSet rs) throws SQLException {
        rs.close();
    }

    public void closePreparedStatement(PreparedStatement ps) throws SQLException {
        ps.close();
    }
}

public class Oracle_Connect_test3 {
    GetDataClass objGetData = new GetDataClass();
    //查询用户是否已经注册   @code=用户ID
    private int getUser(String code) throws SQLException {
        int num = 0;
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            conn = objGetData.getOracleConn();
            String strSql = "Select 用户ID From 用户表 Where 用户ID=?";
            ps = conn.prepareStatement(strSql);
            ps.setString(1, code);
            rs = ps.executeQuery();
            //存在返回 1
            if (rs.next()) {
                num = 1;
            } else {
                num = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            objGetData.closePreparedStatement(ps);
            objGetData.closeResultSet(rs);
            objGetData.closeConnection(conn);
        }
        return num;
    }
//添加用户
    private int addUser() throws SQLException {
        int num = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = objGetData.getOracleConn();
                //EMAIL  必须大写 和数据 保持一致   sysdate
            String strSql = "Insert into 用户表(用户ID,用户名,密码,EMAIL,用户类型) Values( ?,?,?,?,?)";
            ps = conn.prepareStatement(strSql);
            ps.setString(1, "8207919");
            ps.setString(2, "Tony 哥");
            ps.setString(3, "6688");
            ps.setString(4, "yhj1610@com");
            ps.setString(5, "5");
            num = ps.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            objGetData.closePreparedStatement(ps);
            objGetData.closeConnection(conn);
        }
        return num;
    }

//修改用户密码  @password=新密码   @code=用户ID号
    private int updatePassword(String password , String code) throws SQLException {
        int num = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = objGetData.getOracleConn();
            String strSql = "Update 用户表 Set 密码=? Where 用户ID=?";
            ps = conn.prepareStatement(strSql);
            ps.setString(1, password);
            ps.setString(2, code);
            num = ps.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            objGetData.closePreparedStatement(ps);
            objGetData.closeConnection(conn);
        }
        return num;
    }


    public static void main(String[] args) throws SQLException {
        Oracle_Connect_test3 ja10_5 = new Oracle_Connect_test3();
        //先查询数据库是否存在 这个用户   参数 =用户ID
        if (ja10_5.getUser("8207919") > 0) {
            //已经存在就修改密码
            JOptionPane.showMessageDialog(null, "该用户已注册过!");
               //9988111  新的密码   |   8207919  用户id
            if (ja10_5.updatePassword("q123", "8207919") > 0) {
                JOptionPane.showMessageDialog(null, "修改密码成功!");
            }
        } else {
            if (ja10_5.addUser() > 0) {
                JOptionPane.showMessageDialog(null, "注册成功!");
            }
        }
    }
}
posted @ 2020-02-12 21:08  Tony小哥  阅读(217)  评论(2编辑  收藏  举报