1. 程式人生 > >Java操作Oracle並實現增刪改查

Java操作Oracle並實現增刪改查

在這裡我選擇的是maven專案,但是找了良久沒有發現依賴,或者就是不能用,於是就去Oracle官網下載了ojdbc的jar包.

連線程式碼如下:

package oracle;

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

/**
 * @Author: LiGe
 * @Date: 2018/10/21 11:50
 * @description: Oracle連線工具類
 */
public class DBUtil {
    //資料庫連線地址
    private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
    //使用者名稱
    private static String username= "system";
    //密碼
    private static String password ="ZZZ111";
    //驅動名稱
    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("資料庫連線失敗");
            }
        }

}

下面是增刪改查

package oracle;

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

/**
 * @Author: LiGe
 * @Date: 2018/10/21 13:21
 * @description:
 */
public class OracleDemo {
    //連線物件
    Connection connection=null;
    //建立預編譯物件
    PreparedStatement ps=null;
    //建立結果集
    ResultSet rs = null;

    /*插入*/
    public int insert(){
        int result = 0;
        connection = DBUtil.getCon();
        String sql = "insert into student values(?,?,?)";
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1,2);
            ps.setString(2,"老王");
            ps.setString(3,"女");
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeCon(connection);
        }
        return result;
    }
    /*查詢*/
    public void select(){
        connection = DBUtil.getCon();
        String sql = "select id,name,gender from student";
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String gender = rs.getString(3);
                System.out.println("ID:"+id + " NAME:"+name+" GENDER:"+gender);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeCon(connection);
        }
    }
    /*修改*/
    public int update(){
        connection = DBUtil.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 {
            DBUtil.closeCon(connection);
        }

        return result;

    }

    /*刪除 */
    public int delete(){
        int result = 0;
        connection = DBUtil.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 {
            DBUtil.closeCon(connection);
        }
        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());
    }
}