写给小白~使用java利用ojdbc连接oracle数据库实现增删查改

1 篇文章 0 订阅

今天帮一小白写了个作业,也好久没用过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();
    }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值