寫給小白~使用java利用ojdbc連接oracle數據庫實現增刪查改

今天幫一小白寫了個作業,也好久沒用過oracle了,屏內盡代碼,窗外有霓虹,願每個追夢人都能成功吧。

 1.獲取連接工具類

public class ConnectionManager {
    static {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:oracle:thin:@//localhost:1521/orcl.microdone.cn", "scott", "123456");
    }

    public static void closeConnection(ResultSet rs, Statement ps,
                                       Connection conn) throws SQLException {
        try {
            if (rs != null)
                rs.close();
        } finally {
            try {
                if (ps != null)
                    ps.close();
            } finally {
                if (conn != null)
                    conn.close();
            }
        }
    }

}
public class Student {

    private String id;

    private String studentName;

    private String studentSex;

    private Integer studentAge;

    private String studentAddress;

    private String birthday;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentSex() {
        return studentSex;
    }

    public void setStudentSex(String studentSex) {
        this.studentSex = studentSex;
    }

    public Integer getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(Integer studentAge) {
        this.studentAge = studentAge;
    }

    public String getStudentAddress() {
        return studentAddress;
    }

    public void setStudentAddress(String studentAddress) {
        this.studentAddress = studentAddress;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class StudentHandler {


    private static String CREATE_URL="CREATE TABLE student\n" +
            "   (\tid VARCHAR2(32) NOT NULL ENABLE, \n" +
            "\tstudentName VARCHAR2(20) NOT NULL ENABLE, \n" +
            "\tstudentSex VARCHAR2(2), \n" +
            "\tstudentAge NUMBER(*,0), \n" +
            "\tstudentAddress VARCHAR2(30), \n" +
            "\tbirthday VARCHAR2(10), \n" +
            "\t PRIMARY KEY (id)\n" +
            "  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOCOMPRESS LOGGING\n" +
            "  TABLESPACE USERS  ENABLE\n" +
            "   ) SEGMENT CREATION DEFERRED \n" +
            "  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING\n" +
            "  TABLESPACE USERS";

    /**
     * 創建表
     * @param sql
     */
    public void createStudent(String sql){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            ps = conn.prepareStatement(sql);
            ps.executeUpdate(sql);
            System.out.println("創建表成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 新增學生
     * @param student
     */
    public void addStudent(Student student) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            String sql = "insert into  student values (?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, student.getId());
            ps.setString(2, student.getStudentName());
            ps.setString(3, student.getStudentSex());
            ps.setInt(4, student.getStudentAge());
            ps.setString(5, student.getStudentAddress());
            ps.setString(6, student.getBirthday());
            int len = ps.executeUpdate();
            if (len > 0)
                System.out.println("添加學生成功!");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 刪除學生
     * @param student
     */
    public void deleteStudent(Student student) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            String sql = "delete from student where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, student.getId());
            int len = ps.executeUpdate();
            if (len > 0)
                System.out.println("刪除學生成功!");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 刪除全部
     */
    public void deleteStudentAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            String sql = "delete from student ";
            ps = conn.prepareStatement(sql);
            int len = ps.executeUpdate();
            if (len > 0)
                System.out.println("刪除全部學生成功!");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查詢全部
     * @param student
     */
    public void queryStudent(Student student) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            String sql = "select * from  student ";
            ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery(sql);
            System.out.println(11);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新
     * @param student
     */
    public void updateStudent(Student student) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = ConnectionManager.getConnection();
            String sql = "update student set STUDENTNAME=?,STUDENTSEX=?,STUDENTAGE=?,STUDENTADDRESS=?,BIRTHDAY=? where id=?";

            ps = conn.prepareStatement(sql);
            ps.setString(1, student.getStudentName());
            ps.setString(2, student.getStudentSex());
            ps.setInt(3, student.getStudentAge());
            ps.setString(4, student.getStudentAddress());
            ps.setString(5, student.getBirthday());
            ps.setString(6, student.getId());
            int rs = ps.executeUpdate();
            System.out.println("修改數據條數:"+rs);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ConnectionManager.closeConnection(null, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        StudentHandler studentHandler = new StudentHandler();
        //創建用戶
        //studentHandler.createStudent(StudentHandler.CREATE_URL);
        Student student = new Student();
        student.setId(UUID.randomUUID().toString().replace("-",""));
        student.setBirthday("2020-06-25");
        student.setStudentAddress("濟南");
        student.setStudentAge(12);
        student.setStudentName("zzZ");
        student.setStudentSex("1");
        studentHandler.addStudent(student);
        student.setBirthday("2020-06-24");
        studentHandler.updateStudent(student);
        studentHandler.deleteStudent(student);
        studentHandler.deleteStudentAll();
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章