sun公司开发一组标准api,他们只是接口,并没有提供实现类,由数据库厂商提供实现类,即驱动程序
jdbc操作过程:1.jar包导入;2.定义记录的类(如student类);3.连接的获取;4.sql的执行 。
// sql的执行 // insertprivate 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;}// updateprivate 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 tablenameprivate 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;}// deleteprivate 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: 1resutl: 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的操作的详细内容。
