JDBC操作数据库的增加、删除、更新、查找实例分析
本文实例讲述了JDBC操作数据库的增加、删除、更新、查找方法。分享给大家供大家参考,具体如下:
packagecn.com.JDBC; importjava.sql.Connection; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.sql.Statement; publicclassCRUD{ publicstaticvoidmain(String[]args)throwsSQLException{ //TODOAuto-generatedmethodstub //create(); //update(); delete(); read(); } staticvoiddelete()throwsSQLException { Connectionconn=null; Statementst=null; ResultSetresultset=null; try{ //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 Stringsql="deletefromuserwhereid>5"; inti=st.executeUpdate(sql); System.out.println("i="+i); }finally { JdbcUtils.free(resultset,st,conn); } } staticvoidupdate()throwsSQLException { Connectionconn=null; Statementst=null; ResultSetresultset=null; try{ //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 Stringsql="updateusersetmoney=money+20"; inti=st.executeUpdate(sql); System.out.println("i="+i); }finally { JdbcUtils.free(resultset,st,conn); } } staticvoidcreate()throwsSQLException { Connectionconn=null; Statementst=null; ResultSetresultset=null; try{ //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 Stringsql="insertintouser(name,birthday,money)values('wy','2011-09-23','2894656')"; inti=st.executeUpdate(sql); System.out.println("i="+i); }finally { JdbcUtils.free(resultset,st,conn); } } staticvoidread()throwsSQLException { Connectionconn=null; Statementst=null; ResultSetresultset=null; try{ //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 resultset=st.executeQuery("selectid,name,birthday,moneyfromuser"); //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); } } } packagecn.com.JDBC; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.sql.Statement; publicclassJdbcUtils { privatestaticStringurl="jdbc:mysql://localhost:3306/jdbc"; privatestaticStringuser="root"; privatestaticStringpassword="123"; privateJdbcUtils() { } static { try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundExceptione) { thrownewExceptionInInitializerError(e); } } publicstaticConnectiongetConnection()throwsSQLException { returnDriverManager.getConnection(url,user,password); } publicstaticvoidfree(ResultSetresultset,Statementst,Connectionconn) { //6.释放资源 try{ if(resultset!=null) resultset.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } finally { try { if(st!=null) st.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } finally { if(conn!=null) try{ conn.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } } }
希望本文所述对大家Java程序设计有所帮助。