SQL数据库连接工具类
2014-12-15 00:00
316 查看
package cn.itcast; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 查询结果处理接口 * * @author 作者:LiuJunGuang * @version 创建时间:2011-11-10 下午07:26:41 */ interface ResultSetHander { Object hander(ResultSet rs); } /** * 类说明:数据库连接和释放 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-6 上午11:49:14 */ public class JDBCUtils { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/chat"; private static String user = "root"; private static String password = "root"; private static Connection con = null; // 注册驱动 static { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 创建连接 public static Connection getConnection() { if (con == null) { try { con = DriverManager.getConnection(url, user, password); } catch (SQLException e) { throw new RuntimeException(e); } } return con; } // 关闭连接 public static void colse(ResultSet rs, Statement st, Connection con) { if (rs != null) { try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (st != null) { try { st.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (con != null) { try { con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } // 更新数据库 public static void update(String sql, Object[] params) { Connection con = JDBCUtils.getConnection(); Statement stmt = null; ResultSet rs = null; try { PreparedStatement ps = con.prepareStatement(sql); ParameterMetaData psm = ps.getParameterMetaData();// 获得查询语句的元数据信息 int paramNum = psm.getParameterCount();// 得到参数的总个数 for (int i = 1; i <= paramNum; i++) { ps.setObject(i, params[i - 1]); } ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { JDBCUtils.colse(rs, stmt, con); } } // 查询数据库 public static Object query(String sql, Object[] params, ResultSetHander rsh) { Connection con = JDBCUtils.getConnection(); Statement stmt = null; ResultSet rs = null; try { PreparedStatement ps = con.prepareStatement(sql); ParameterMetaData psm = ps.getParameterMetaData();// 获得查询语句的元数据信息 int paramNum = psm.getParameterCount();// 得到参数的总个数 for (int i = 1; i <= paramNum; i++) { ps.setObject(i, params[i - 1]); } rs = ps.executeQuery(); return rsh.hander(rs); } catch (SQLException e) { throw new RuntimeException(e); } finally { JDBCUtils.colse(rs, stmt, con); } } }
package cn.itcast; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; /** * 类说明:数据库操作类 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-6 下午12:02:36 */ public class JDBCUtilsTest { @Test public void find() { String sql = "select * from history"; JDBCUtils.query(sql, null, new ResultSetHander() { @Override public Object hander(ResultSet rs) { try { while (rs.next()) { System.out.println(rs.getString("username")); } } catch (SQLException e) { e.printStackTrace(); } return null; } }); } @Test public void update() { String sql = "delete from history where id = ?"; Object obj[] = { 1 }; JDBCUtils.update(sql, obj); } @Test public void find2() { Connection con = JDBCUtils.getConnection(); PreparedStatement st = null; ResultSet rs = null; try { String find = "select * from history where username=? and action=?"; st = con.prepareStatement(find); st.setString(1, "nihao"); st.setString(2, "微笑着"); rs = st.executeQuery(); while (rs.next()) { System.out.println(rs.getString("username")); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.colse(rs, st, con); } } }
相关文章推荐
- SQL数据库连接工具类
- SQL数据库连接工具类
- JDBC连接数据库工具类及连接Access数据库示例
- Http网络连接框架工具类(基于volley.jar和Xutils.jar)
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,
- NetUtils网络连接工具类
- asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)
- Visual C#连接、操作SQL数据库详细教程——疑问解答篇
- C# 连接SQL数据库以及操作数据库
- PHP连接Sql数据库的相关方法
- 网络连接工具类
- 登录界面与SQL数据库连接
- MFC中连接sql数据库的简单方法
- ADO.NET连接SQL数据库大总结
- 网络连接底层工具类,执行get和post请求(用于在一个项目中去对另外项目发出请求)
- Android 网络连接工具类
- 连接数据库的工具类
- FAQ-1:不能远程连接其它SQL数据库问题及解决方法
- sql数据库java连接sqlserver2005数据库
- C++连接SQL数据库第一步 系统配置