JDBC操作数据库的增加、删除、更新、查找实例分析
程序员文章站
2024-03-06 11:53:37
本文实例讲述了jdbc操作数据库的增加、删除、更新、查找方法。分享给大家供大家参考,具体如下:
package cn.com.jdbc;
import java...
本文实例讲述了jdbc操作数据库的增加、删除、更新、查找方法。分享给大家供大家参考,具体如下:
package cn.com.jdbc; import java.sql.connection; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class crud { public static void main(string[] args) throws sqlexception { // todo auto-generated method stub //create(); //update(); delete(); read(); } static void delete() throws sqlexception { connection conn=null; statement st=null; resultset resultset=null; try { //2.建立连接 conn=jdbcutils.getconnection(); //单例设计模式 conn=jdbcutilssingle.getinstance().getconnection(); //3.创建语句 st=conn.createstatement(); //4.执行语句 string sql="delete from user where id>5"; int i=st.executeupdate(sql); system.out.println("i="+i); } finally { jdbcutils.free(resultset, st, conn); } } static void update() throws sqlexception { connection conn=null; statement st=null; resultset resultset=null; try { //2.建立连接 conn=jdbcutils.getconnection(); //单例设计模式 conn=jdbcutilssingle.getinstance().getconnection(); //3.创建语句 st=conn.createstatement(); //4.执行语句 string sql="update user set money=money+20"; int i=st.executeupdate(sql); system.out.println("i="+i); } finally { jdbcutils.free(resultset, st, conn); } } static void create() throws sqlexception { connection conn=null; statement st=null; resultset resultset=null; try { //2.建立连接 conn=jdbcutils.getconnection(); //单例设计模式 conn=jdbcutilssingle.getinstance().getconnection(); //3.创建语句 st=conn.createstatement(); //4.执行语句 string sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')"; int i=st.executeupdate(sql); system.out.println("i="+i); } finally { jdbcutils.free(resultset, st, conn); } } static void read() throws sqlexception { connection conn=null; statement st=null; resultset resultset=null; try { //2.建立连接 conn=jdbcutils.getconnection(); //单例设计模式 conn=jdbcutilssingle.getinstance().getconnection(); //3.创建语句 st=conn.createstatement(); //4.执行语句 resultset=st.executequery("select id,name,birthday,money from user"); //5.处理结果 while(resultset.next()) { system.out.println(resultset.getobject("id")); system.out.println(resultset.getobject("name")); system.out.println(resultset.getobject("birthday")); system.out.println(resultset.getobject("money")); } } finally { jdbcutils.free(resultset, st, conn); } } } package cn.com.jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class jdbcutils { private static string url="jdbc:mysql://localhost:3306/jdbc"; private static string user="root"; private static string password="123"; private jdbcutils() { } static { try { class.forname("com.mysql.jdbc.driver"); } catch(classnotfoundexception e) { throw new exceptionininitializererror(e); } } public static connection getconnection() throws sqlexception { return drivermanager.getconnection(url, user, password); } public static void free(resultset resultset,statement st,connection conn) { //6.释放资源 try{ if(resultset!=null) resultset.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { try { if(st!=null) st.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { if(conn!=null) try { conn.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } }
希望本文所述对大家java程序设计有所帮助。