JDBC工具包

来源:互联网 发布:cms设计文档 编辑:程序博客网 时间:2024/06/07 11:10
package com.neuedu.servlet;


import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class Jdbcutil {
//JDBC的连接方法
public static Connection getConnection() {
Connection con=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//JDBC的关闭方法
public static void close(Connection con){

try {
if(con!=null){
con.close();//关闭连接
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
//插入方法
public static int insert(String sql,Object[] params){
int result=0;

Connection con=getConnection();
try {
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
result=pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(con!=null){
close(con);
}
}

return result;
  }
//更新方法
public static int update(String sql,Object[] params){
int result=0;
Connection con=getConnection();
try {
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
result=pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(con);
}

return result;
}
//删除方法
public static int delete(String sql,Object[] params){
Connection con=Jdbcutil.getConnection();
int a=0;
try {
PreparedStatement pstms=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstms.setObject(i+1, params[i]);
}
a=pstms.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {


if(con!=null){
close(con);
}
}
return a;
}
//通过ID查找用户是否存在
public static int findById(String sql,int id){
int result=0;
Connection conn=getConnection();
try {
PreparedStatement prepareStatement = conn.prepareStatement(sql);
prepareStatement.setInt(1, id);
ResultSet rs = prepareStatement.executeQuery();
if(rs.next()){
result=1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//通过姓名查找用户是否存在
public static int findByName(String sql,String username){
int result=0;
Connection conn=getConnection();
try {
PreparedStatement prepareStatement = conn.prepareStatement(sql);
prepareStatement.setString(1, username);
ResultSet rs = prepareStatement.executeQuery();
if(rs.next()){
result=1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//通过姓名密码找是否用户存在
public static int findByNameAndPassword(String sql,String username,String password){
int result=0;
Connection conn=getConnection();
try {
PreparedStatement prepareStatement = conn.prepareStatement(sql);
prepareStatement.setString(1, username);
prepareStatement.setString(2, password);
ResultSet rs = prepareStatement.executeQuery();
if(rs.next()){
result=1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
原创粉丝点击