Java基于jdbc实现的增删改查操作示例
本文实例讲述了Java基于jdbc实现的增删改查操作。分享给大家供大家参考,具体如下:
增删改操作:
packagejava_web; importjava.sql.DriverManager; importjava.sql.SQLException; importcom.mysql.jdbc.Connection; importcom.mysql.jdbc.Statement; /** *jdbcCURD *@authorAdministrator * */ publicclassrbacDemo{ publicfinalstaticStringURL="jdbc:mysql://localhost:3306/test"; publicfinalstaticStringUSERNAME="root"; publicfinalstaticStringPASSWORD=""; publicfinalstaticStringDRIVER="com.mysql.jdbc.Driver"; /** *插入 */ publicstaticvoidinsert(){ try{ Class.forName(DRIVER); Connectionconn=(Connection)DriverManager.getConnection(URL,USERNAME,PASSWORD); Stringsql="insertintotest(name,sex)values('fifi2',1),('fifi3',3)"; Statementstate=(Statement)conn.createStatement(); intresult=state.executeUpdate(sql); state.close(); conn.close(); System.out.println(result+"success"); }catch(ClassNotFoundExceptione){ //TODO自动生成的catch块 e.printStackTrace(); }catch(SQLExceptione){ //TODO自动生成的catch块 e.printStackTrace(); } } publicstaticvoidupdate(){ try{ Class.forName(DRIVER); Connectionconn=(Connection)DriverManager.getConnection(URL,USERNAME,PASSWORD); Stringsql="updatetestsetname='fifi3aaa'wherename='fifi3'"; Statementstate=(Statement)conn.createStatement(); intresult=state.executeUpdate(sql); state.close(); conn.close(); System.out.println(result+"success"); }catch(ClassNotFoundExceptione){ //TODO自动生成的catch块 e.printStackTrace(); }catch(SQLExceptione){ //TODO自动生成的catch块 e.printStackTrace(); } } publicstaticvoiddelete(){ try{ Class.forName(DRIVER); Connectionconn=(Connection)DriverManager.getConnection(URL,USERNAME,PASSWORD); Stringsql="deletefromtestwherename='fifi3aaa'"; Statementstate=(Statement)conn.createStatement(); intresult=state.executeUpdate(sql); state.close(); conn.close(); System.out.println(result+"success"); }catch(ClassNotFoundExceptione){ //TODO自动生成的catch块 e.printStackTrace(); }catch(SQLExceptione){ //TODO自动生成的catch块 e.printStackTrace(); } } publicstaticvoidmain(String[]args){ //insert(); //update(); delete(); } }
查询操作:
packagejava_web; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; importcom.mysql.jdbc.Connection; importcom.mysql.jdbc.Statement; publicclassjdbcQueryDemo{ publicfinalstaticStringURL="jdbc:mysql://localhost:3306/test"; publicfinalstaticStringUSERNAME="root"; publicfinalstaticStringPASSWORD=""; publicfinalstaticStringDRIVER="com.mysql.jdbc.Driver"; publicstaticvoidquery(){ try{ Class.forName(DRIVER); Connectionconn=(Connection)DriverManager.getConnection(URL,USERNAME,PASSWORD); Stringsql="selectid,name,sexfromtestwhereid=3"; Statementstate=(Statement)conn.createStatement(); ResultSetrs=state.executeQuery(sql); while(rs.next()){ //rs.getInt("id"); intid=rs.getInt(1); Stringname=rs.getString(2); intsex=rs.getInt(3); //Stringtime=rs.getString("vtime"); System.out.println(id+"=="+name+"=="+sex+"=="); } rs.close(); state.close(); conn.close(); }catch(ClassNotFoundExceptione){ //TODO自动生成的catch块 e.printStackTrace(); }catch(SQLExceptione){ //TODO自动生成的catch块 e.printStackTrace(); } } publicstaticvoidmain(String[]args){ //TODO自动生成的方法存根 query(); } }
更多关于java相关内容感兴趣的读者可查看本站专题:《Java使用JDBC操作数据库技巧总结》、《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。