日期:2014-05-16 浏览次数:20515 次
万变不离其宗。。持久层怎么变也是JDBC,框架怎么新也是反射机制。。
?
今天刚好复习一下JDBC,顺便对MySql进行一个入门的学习。
?
环境:MySql 5.5 + Navicat for MySql 10.0.5 + MyEclipse 9.0
?
从MySql官方:http://www.mysql.com/?下载了 mysql-connector-java-5.1.17-bin.jar
?
从MyEclipse的DB Browser中得到测试成功后的
?
驱动类:com.mysql.jdbc.Driver
链接URL:jdbc:mysql://localhost:3306/accp
?
准备的差不多了,实例就是 简单粗暴,直接有效 直接上代码。。
?
---------------------------------------我是华丽的无所不在的分割线-------------------------------------------
?
?
用户实体类:
package com.accp.jdbc.entity; /** * * @author Maxpin on 2011-10-04 * * 用户实体类 */ public class Userinfo { private int userid; // 编号 private String loginid; // 用户名 private String loginpwd; // 密码 private String username; // 姓名 /** * 构造方法 */ public Userinfo() { } /** * @param loginid * @param loginpwd * @param username */ public Userinfo(String loginid, String loginpwd, String username) { this.loginid = loginid; this.loginpwd = loginpwd; this.username = username; } /** * @param userid * @param loginid * @param loginpwd * @param username */ public Userinfo(int userid, String loginid, String loginpwd, String username) { this.userid = userid; this.loginid = loginid; this.loginpwd = loginpwd; this.username = username; } //getter & setter methods 略?
?
?
Dao基类:包含了数据库链接、关闭、CRUD操作及MySql分页查询
package com.accp.jdbc.base; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.accp.jdbc.entity.Userinfo; /** * * @author Maxpin on 2011-10-04 * * Dao基类:包含了数据库链接、关闭、CRUD操作及MySql分页查询 */ public class BaseDao { // 连接地址 private static final String url = "jdbc:mysql://localhost:3306/accp"; // 驱动类 private static final String driverClass = "com.mysql.jdbc.Driver"; // 用户名 private static final String uname = "root"; // 密码 private static final String pwd = "admin"; /** * 获取数据库连接 * * @return 连接对象 */ protected static Connection getConnection() { Connection conn = null; try { Class.forName(driverClass); conn = DriverManager.getConnection(url, uname, pwd); } catch (ClassNotFoundException e) { System.out.println("找不到驱动类"); } catch (SQLException e) { System.out.println("建立连接错误!"); } return conn; } /** * 关闭数据库连接 * * @param conn * 数据库连接 * @param rs * 结果集 * @param pstmt * 命令对象 */ public static void closeAll(Connection conn, ResultSet rs, Statement pstmt) { try { if (null != rs && !rs.isClosed()) { rs.close(); rs = null; } } catch (SQLException e) { System.out.println("关闭结果集出错!"); } try { if (null != pstmt && !pstmt.isClosed()) { pstmt.close(); pstmt = null; } } catch (SQLException e) { System.out.println("关闭命令对象出错!"); } try { if (null != conn && !conn.isClosed()) { conn.close(); conn = null; } } catch (SQLException e) { System.out.println("关闭链接出错"); } } /** * 保存指定用户信息 * * @param user * 用户对象 * @throws Exception * 抛出异常 */ public static void saveUserinfo(Userinfo user) throws Exception { if (null != user) { Connection conn = getConnection(); PreparedStatement pstmt = null; String sql = "insert into USERINFO values(null,?,?,?)"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, user.getLoginid()); pstmt.setString(2, user.getLoginpwd()); pstmt.setString(3, user.getUsername()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, null, pstmt); } } else { throw new Exception("用户信