Java的SqlHelper 工具类
来源:互联网 发布:关系数据库的范式 编辑:程序博客网 时间:2024/06/08 00:47
原文地址
- <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java"></pre><pre name="code" class="java">context.xml中添加以下内容
- <Resource name="jdbc/TestDB" auth="Container"
- type="javax.sql.DataSource"
- maxActive="100" maxIdle="30" maxWait="10000"
- username="root" password="hufan.88"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://127.0.0.1:3306/hoofan"/>
- import java.sql.*;
- import java.util.ArrayList;
- import javax.naming.*;
- import javax.sql.DataSource;
- public class SqlHelper {
- private static DataSource ds;
- static{
- try {
- //1.获取命名上下文接口
- Context context = new InitialContext();
- //2.根据名称查询服务器上的DataSource 代表前缀: java:/comp/env
- ds = (DataSource)context.lookup("java:/comp/env/jdbc/TestDB");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 获取数据库连接
- * @return Connection
- * */
- public static Connection getConnection(){
- try {
- return ds.getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 关闭资源
- * @param Connection con,Statement stm,ResultSet rs
- * */
- public static void close(ResultSet rs,PreparedStatement ps,Connection ct){
- try {
- if(rs!=null)rs.close();
- if(ps!=null)ps.close();
- if(ct!=null)ct.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- public ArrayList executeQuery(String sql,String[] arr){
- Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- ct=getConnection();
- ps=ct.prepareStatement(sql);
- if(arr!=null&&!arr.equals("")){
- for(int i=0;i<arr.length;i++){
- ps.setString(i+1, arr[i]);
- }
- }
- rs=ps.executeQuery();
- ArrayList al=new ArrayList();
- ResultSetMetaData rsmd=rs.getMetaData();
- int column=rsmd.getColumnCount();
- while(rs.next()){
- Object[] ob=new Object[column];
- for(int i=0;i<ob.length;i++){
- ob[i]=rs.getObject(i+1);
- }
- al.add(ob);
- }
- return al;
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- return null;
- }finally{
- close(rs,ps,ct);
- }
- }
- public void executeUpdate(String sql,String arr[]){
- Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- ct=getConnection();
- ps=ct.prepareStatement(sql);
- if(arr!=null&&!arr.equals("")){
- for(int i=0;i<arr.length;i++){
- ps.setString(i+1, arr[i]);
- }
- }
- ps.executeUpdate();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }</pre><br><br></pre><pre name="code" class="java">//////////////////////////////////////////////////////////////////////////////////////////////////////////////</pre><pre name="code" class="java">package com.utils;
- import java.sql.*;
- import java.util.*;
- public class SqlHelper {
- static{
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private Connection getConnection(){
- try {
- return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hufan","root","hufan.88");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
- }
- public static void close(ResultSet rs,PreparedStatement ps,Connection ct){
- try {
- if(rs!=null)rs.close();
- if(ps!=null)ps.close();
- if(ct!=null)ct.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- public ArrayList executeQuery(String sql,String[] arr){
- Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- ct=getConnection();
- ps=ct.prepareStatement(sql);
- if(arr!=null&&!arr.equals("")){
- for(int i=0;i<arr.length;i++){
- ps.setString(i+1, arr[i]);
- }
- }
- rs=ps.executeQuery();
- ArrayList al=new ArrayList();
- ResultSetMetaData rsmd=rs.getMetaData();
- int column=rsmd.getColumnCount();
- while(rs.next()){
- Object[] ob=new Object[column];
- for(int i=0;i<ob.length;i++){
- ob[i]=rs.getObject(i+1);
- }
- al.add(ob);
- }
- return al;
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- return null;
- }finally{
- close(rs,ps,ct);
- }
- }
- public void executeUpdate(String sql,String arr[]){
- Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- ct=getConnection();
- ps=ct.prepareStatement(sql);
- if(arr!=null&&!arr.equals("")){
- for(int i=0;i<arr.length;i++){
- ps.setString(i+1, arr[i]);
- }
- }
- ps.executeUpdate();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
- </pre><br><br>
0 0
- Java的SqlHelper 工具类
- Java的SqlHelper 工具类
- SqlHelper工具类
- SqlHelper工具类
- 编写Web时可能会用的工具类----SqlHelper.java(韩顺平)
- Java的一个SQLHelper帮助类
- Java访问MySQL数据库的SqlHelper类
- JAVA WEB SQLHelper类的封装
- SqlHelper操纵数据库工具类
- 微软的SQLHelper类
- 微软的SqlHelper类
- SqlHelper类的实现
- 强大的SQLHelper类
- SqlHelper类的使用
- 最新的SqlHelper 类
- SqlHelper类的编写
- 简单的sqlHelper类
- Java访问MySQL数据库的SqlHelper类以及测试程序
- 中移动4G资费¥48000/h
- 蓝桥杯-----世纪末的星期
- 微信7个非常有用的功能
- 学习Java07
- Android学习小Demo(7)SharedPreference的使用
- Java的SqlHelper 工具类
- 初学linux
- 计算器
- MFC笔记 MFC与Access连接的实现
- 数据库开发(4)E-R模型
- 鸟哥的linux私房菜 mark1
- 安卓完全退出程序的六种方法
- 单链表的C++实现 Implement of Linked List
- 导入项目一直显示Select at least one project