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;
}
}
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;
}
}
阅读全文
0 0
- JDBC工具包
- JDBC通用工具包
- JDBC连接数据库的工具包代码
- Apache DbUtils - JDBC轻量级封装的工具包
- java连接库的JDBC工具包
- 【java基础:JDBC】基于DPCP连接池工具包和DBUtils工具包的数据库改查Demonstration
- 发现一个操作JDBC的好的apache工具包
- JDBC轻量级封装的工具包Apache DbUtils使用教程
- 一个直接获取JDBC到数据库连接的工具包(二)
- 写一个直接获取JDBC到数据库连接的工具包(一)
- java工具包
- “常用工具包”
- Silverlight工具包
- 工具包:ClassUtils
- 工具包:ZipUtils
- Java 工具包
- plsql 工具包
- libpcap工具包
- Angular4的ng2-ckeditor富文本编辑框的使用
- oninput和onchange在input框里触发函数的问题
- Wordpress 的SMTP的邮件发送 插件WP Email SMTP
- ios 模仿系统相册 预览
- Manacher算法总结
- JDBC工具包
- SQL 语法
- 【数据库】Oracle语句练习
- vc++编译时运行库选择(/MT、/MTd、/MD、/MDd)
- git中refs/for 和refs/heads
- 编程思想的理解(POP,OOP,SOA,AOP)
- valgirnd的简单使用
- Web23.DOM操作
- 新路程------hi3516a标准简单版i2c设备驱动