Java的SqlHelper 工具类
2014-02-08 23:28
302 查看
原文地址
<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>
<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>
相关文章推荐
- Java的SqlHelper 工具类
- 编写Web时可能会用的工具类----SqlHelper.java(韩顺平)
- (java 功能篇) Java MySQL数据库连接工具类
- java 并发工具类-Semaphore
- 用java自带的工具类ResourceBundle类读取.properties配置文件的工具类
- java MD5Utils 加密工具类
- java工具类(五)之日期格式字符串与日期实现互转
- (二十九)Java工具类ThreadUtils详解
- java获取url连接地址参数的工具类
- JAVA中使用FTPClient工具类上传下载
- (三十)Java工具类SerializationUtils源码详解
- JAVA-Properties类和Collections工具类
- java工具类开发之json工具类JsonUtils和写数据回页面工具类ResponseUtils
- Java实现--密码强度检测--的工具类代码
- java 使用redis的工具类
- Java日期工具类大全
- java的File的工具类
- java日期工具类2
- Java-FileUtil工具类
- java日期处理工具类