欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

深入浅出JAVA开发中JDBC的操作

程序员文章站 2022-03-16 22:13:06
...
Sun公司开发一组标准api,他们只是接口,并没有提供实现类,由数据库厂商提供实现类,即驱动程序

jdbc操作过程:1.jar包导入;2.定义记录的类(如Student类);3.连接的获取;4.sql的执行 。

// sql的执行 
// insert
private static int insert(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setString(1, student.getName());
        pstmt.setString(2, student.getSex());
        pstmt.setString(3, student.getAge());
        i = pstmt.executeUpdate();
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}

// update
private static int update(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        i = pstmt.executeUpdate();
        System.out.println("resutl: " + i);
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}
// select xx from tableName
private static Integer getAll() {
    Connection conn = getConn();
    String sql = "select * from students";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement)conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        int col = rs.getMetaData().getColumnCount();
        System.out.println("============================");
        while (rs.next()) {
            for (int i = 1; i <= col; i++) {
                System.out.print(rs.getString(i) + "\t");
                if ((i == 2) && (rs.getString(i).length() < 8)) {
                    System.out.print("\t");
                }
             }
            System.out.println("");
        }
            System.out.println("============================");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

// delete
private static int delete(String name) {
    Connection conn = getConn();
    int i = 0;
    String sql = "delete from students where Name='" + name + "'";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        i = pstmt.executeUpdate();
        System.out.println("resutl: " + i);
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}

测试:

// 测试
public static void main(String args[]) {
    JDBCOperation.getAll();
    JDBCOperation.insert(new Student("Achilles", "Male", "14"));
    JDBCOperation.getAll();
    JDBCOperation.update(new Student("Bean", "", "7"));
    JDBCOperation.delete("Achilles");
    JDBCOperation.getAll();
}

输出结果:

============================
1    Ender        male    8    
2    Bean        male    6    
3    Petra        fema    9    
4    Peter        male    9    
5    _Graff        male    40    
6    GOD        fema    255    
============================
============================
1    Ender        male    8    
2    Bean        male    6    
3    Petra        fema    9    
4    Peter        male    9    
5    _Graff        male    40    
6    GOD        fema    255    
7    Achilles    Male    14    
============================
resutl: 1
resutl: 1
============================
1    Ender        male    8    
2    Bean        male    7    
3    Petra        fema    9    
4    Peter        male    9    
5    _Graff        male    40    
6    GOD        fema    255    
============================

代码分析:

  在上述对数据库进行增删改查的过程中,可以发现其共性部分,即通用的流程:

  (1)创建Connection对象、SQL查询命令字符串;

  (2)对Connection对象传入SQL查询命令,获得PreparedStatement对象;

  (3)对PreparedStatement对象执行executeUpdate()或executeQurey()获得结果;

  (4)先后关闭PreparedStatement对象和Connection对象。

  可见,使用JDBC时,最常打交道的是Connection、PreparedStatement这两个类,以及select中的ResultSet类。

思考问题

1.每次SQL操作都需要建立和关闭连接,这势必会消耗大量的资源开销,如何避免?

分析:可以采用连接池,对连接进行统一维护,不必每次都建立和关闭。事实上这是很多对JDBC进行封装的工具所采用的。

2.Java代码中,传入的数据格式与数据库定义不同怎么办?如把Java的String对象赋值给数据库的tinyint属性。

分析:在执行SQL语句时,数据库会尝试进行转换。根据我的实验,如果用内容为纯字母的String对象传入tinyint的age属性时,会被转化成0。具体转化规则应该和数据库有关。

以上就是深入浅出JAVA开发中JDBC的操作的详细内容,更多请关注其它相关文章!

相关标签: jdbc Connection