java操作mysql实现增删改查的方法
程序员文章站
2024-02-27 13:49:33
本文实例讲述了java操作mysql实现增删改查的方法。分享给大家供大家参考,具体如下:
首先,需要把mysql与java连接的jar(mysql-connector-j...
本文实例讲述了java操作mysql实现增删改查的方法。分享给大家供大家参考,具体如下:
首先,需要把mysql与java连接的jar(mysql-connector-java-5.1.6-bin.jar)包导入工程.
package com.cn.edu; import java.beans.statement; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; public class helloworld { private connection conn = null; preparedstatement statement = null; // connect to mysql void connsql() { string url = "jdbc:mysql://localhost:3306/hello?characterencoding=utf-8"; string username = "root"; string password = "root"; // 加载驱动程序以连接数据库 try { class.forname("com.mysql.jdbc.driver" ); conn = drivermanager.getconnection( url,username, password ); } //捕获加载驱动程序异常 catch ( classnotfoundexception cnfex ) { system.err.println( "装载 jdbc/odbc 驱动程序失败。" ); cnfex.printstacktrace(); } //捕获连接数据库异常 catch ( sqlexception sqlex ) { system.err.println( "无法连接数据库" ); sqlex.printstacktrace(); } } // disconnect to mysql void deconnsql() { try { if (conn != null) conn.close(); } catch (exception e) { system.out.println("关闭数据库问题 :"); e.printstacktrace(); } } // execute selection language resultset selectsql(string sql) { resultset rs = null; try { statement = conn.preparestatement(sql); rs = statement.executequery(sql); } catch (sqlexception e) { e.printstacktrace(); } return rs; } // execute insertion language boolean insertsql(string sql) { try { statement = conn.preparestatement(sql); statement.executeupdate(); return true; } catch (sqlexception e) { system.out.println("插入数据库时出错:"); e.printstacktrace(); } catch (exception e) { system.out.println("插入时出错:"); e.printstacktrace(); } return false; } //execute delete language boolean deletesql(string sql) { try { statement = conn.preparestatement(sql); statement.executeupdate(); return true; } catch (sqlexception e) { system.out.println("插入数据库时出错:"); e.printstacktrace(); } catch (exception e) { system.out.println("插入时出错:"); e.printstacktrace(); } return false; } //execute update language boolean updatesql(string sql) { try { statement = conn.preparestatement(sql); statement.executeupdate(); return true; } catch (sqlexception e) { system.out.println("插入数据库时出错:"); e.printstacktrace(); } catch (exception e) { system.out.println("插入时出错:"); e.printstacktrace(); } return false; } // show data in ju_users void layoutstyle2(resultset rs) { system.out.println("-----------------"); system.out.println("执行结果如下所示:"); system.out.println("-----------------"); system.out.println(" 用户id" + "/t/t" + "淘宝id" + "/t/t" + "用户名"+ "/t/t" + "密码"); system.out.println("-----------------"); try { while (rs.next()) { system.out.println(rs.getint("ju_userid") + "/t/t" + rs.getstring("taobaoid") + "/t/t" + rs.getstring("ju_username") + "/t/t"+ rs.getstring("ju_userpwd")); } } catch (sqlexception e) { system.out.println("显示时数据库出错。"); e.printstacktrace(); } catch (exception e) { system.out.println("显示出错。"); e.printstacktrace(); } } public static void main(string args[]) { helloworld h = new helloworld(); h.connsql(); string s = "select * from ju_users"; string insert = "insert into ju_users(ju_userid,taobaoid,ju_username,ju_userpwd) values("+8329+","+34243+",'mm','789')"; string update = "update ju_users set ju_userpwd =123 where ju_username= 'mm'"; string delete = "delete from ju_users where ju_username= 'mm'"; if (h.insertsql(insert) == true) { system.out.println("insert successfully"); resultset resultset = h.selectsql(s); h.layoutstyle2(resultset); } if (h.updatesql(update) == true) { system.out.println("update successfully"); resultset resultset = h.selectsql(s); h.layoutstyle2(resultset); } if (h.insertsql(delete) == true) { system.out.println("delete successfully"); resultset resultset = h.selectsql(s); h.layoutstyle2(resultset); } h.deconnsql(); } }
notice:
1、现在一般用的驱动是com.mysql.jdbc.driver
,以前的那个什么org的驱动虽然封装了com.mysql.jdbc.driver
,但不好用,过时了。
2、preparestatement(sql)
是statement
的子类,比statement好用。
3、如果数据库中定义的是int值,那么sql语句中要把int单独提出来。如".....values("+8329+","+34243+",'mm','789')"
更多关于java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java文件与目录操作技巧汇总》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。