java SQL连接执行类

package com.zxh.servlet; 

import java.sql.DriverManager; 
import java.sql.SQLException; 

import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.PreparedStatement; 
import com.mysql.jdbc.ResultSet; 
import com.mysql.jdbc.Statement; 

public class SQLBean { 
String dbDriver; 

String connectionString; 

String userName; 

String password; 

Connection connection = null; 

Statement statement = null; 

PreparedStatement preparedStatement = null; 

ResultSet resultSet = null; 

public SQLBean() { 
dbDriver = "com.mysql.jdbc.Driver"; 
connectionString = "jdbc:mysql://localhost/said"; 
userName = "root"; 
password = "123456"; 

} 

public Connection getConnection() { 
try { 
Class.forName(dbDriver); 
System.out.println("Success loading MySQl driver"); 
} catch (ClassNotFoundException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
try { 
connection = (Connection) DriverManager.getConnection( 
connectionString, userName, password); 
System.out.println("Success connection MySQL Database"); 
} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
return connection; 

} 

public void executeInsert(String sqlString) { 
connection = this.getConnection(); 
try { 
statement = (Statement) connection.createStatement(); 
statement.executeUpdate(sqlString); 
} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 

} 

public void executeUpdate(String sqlString) { 
connection = this.getConnection(); 
try { 
statement = (Statement) connection.createStatement(); 

statement.executeUpdate(sqlString); 

} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
// String sql = "select * from student"; 

} 



public ResultSet executeQuery(String sqlString) { 
connection = this.getConnection(); 
try { 
statement = (Statement) connection.createStatement(); 
resultSet = (ResultSet) statement.executeQuery(sqlString); 

} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
return resultSet; 

} 

public void executeDelete(String sqlString) { 
connection = this.getConnection(); 
try { 
statement = (Statement) connection.createStatement(); 
resultSet = (ResultSet) statement.executeQuery(sqlString); 

} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 

} 

public PreparedStatement getPreparedStatement(String sqlString) { 

connection = this.getConnection(); 
try { 
preparedStatement = (PreparedStatement) connection 
.prepareStatement(sqlString); 
} catch (SQLException e) { 
// TODO 自动生成 catch 块 
System.out.println("sqlBean connection error"); 
} 

return preparedStatement; 

} 

public void close() { 
try { 
if (statement != null) 
statement.close(); 
} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
try { 
if (connection != null) 
connection.close(); 
} catch (SQLException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 
} 
} 

public String getDbDriver() { 
return dbDriver; 
} 

public void setDbDriver(String dbDriver) { 
this.dbDriver = dbDriver; 
} 

public String getPassword() { 
return password; 
} 

public void setPassword(String password) { 
this.password = password; 
} 

public String getUserName() { 
return userName; 
} 

public void setUserName(String userName) { 
this.userName = userName; 
} 

public String getConnectionString() { 
return connectionString; 
} 

public void setConnectionString(String connectionString) { 
this.connectionString = connectionString; 
} 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值