📄 db.java
字号:
/**
* DB.java for MySQL
* Connection to MySQLDatabase Method;
* and normal sqlAction,only pass
* all the sqlSentence you need
* to sqlAction in this Package;
* Created on 2006年8月17日, 下午14:13
* @author tangsj
* @version 2.0
*/
package com.glf.reportIE.commons;
import java.sql.*;
import java.util.HashMap;
public class DB {
private String CLASSFORNAME,SERVANDDB,USER,PWD;
private Connection con=null;
private Statement stm;
private PreparedStatement pstm;
public DB() {}
/**
* 执行搜索语句
* @param String sql_select 传入查找的sql语句
* @return ResultSet 返回查找结果
*/
public ResultSet doSelect(String sql_select) throws Exception{
if(con==null){
DataBaseConnection();
}
return stm.executeQuery(sql_select);
}
/**
* 预处理查找语句
* @param sql_select
* @return PreparedStatement
* @throws Exception
*/
public PreparedStatement setSelect(String sql_select) throws Exception{
if(con==null){
DataBaseConnection();
}
return con.prepareStatement(sql_select);
}
public ResultSet exeSelect(PreparedStatement pstm) throws Exception{
if(con==null){
DataBaseConnection();
}
return pstm.executeQuery();
}
/**
* 执行更新语句
* @param String sql_update 传入更新的sql语句
* @return void
*/
public void doUpdate(String sql_update) throws Exception{
if(con==null){
DataBaseConnection();
}
stm.executeUpdate(sql_update);
}
/**
* 预处理update语句
* @param sql_update
* @return
* @throws Exception
*/
public PreparedStatement setUpdate(String sql_update) throws Exception{
if(con==null){
DataBaseConnection();
}
return con.prepareStatement(sql_update);
}
/**
* 执行update
* @param pstm
* @return
* @throws Exception
*/
public int exeUpdate(PreparedStatement pstm) throws Exception{
if(con==null){
DataBaseConnection();
}
return pstm.executeUpdate();
}
/**
* 初始化插入语句
* 例如insert into DatabaseName values('',?,default);
* @param String sql_insert
* @return PreparedStatement
*/
public PreparedStatement setInsert(String sql_insert) throws Exception{
if(con==null){
DataBaseConnection();
}
return con.prepareStatement(sql_insert);
}
/**
* 完成插入语句;通过初始化返回的pstm
* 如pstm.setString(1,form.getName());
* 后传入执行真正插入工作。
* @param PreparedStatement stm_insert
* @return int
*/
public int doInsert(PreparedStatement stm_insert) throws Exception{
return stm_insert.executeUpdate();
}
/**
* 直接执行插入操作,传入插入的sql语句
* 比较适合插入较少内容时的用法
* @param String sql_insert
* @return void
*/
public void excuteInsert(String sql_insert) throws Exception{
if(con==null){
DataBaseConnection();
}
pstm=con.prepareStatement(sql_insert);
pstm.executeUpdate();
// System.out.println(num);
}
/**
* 执行删除操作,传入删除的sql语句
* @param String sql_delete
* @return void
*/
public void doDelete(String sql_delete) throws Exception{
if(con==null){
DataBaseConnection();
}
pstm=con.prepareStatement(sql_delete);
pstm.executeUpdate();
}
/**
* 预处理删除sql语句
* @param sql_delete
* @return
* @throws Exception
*/
public PreparedStatement setDelete(String sql_delete) throws Exception{
if(con==null){
DataBaseConnection();
}
return con.prepareStatement(sql_delete);
}
/**
* 执行删除语句
* @param pstm
* @return
* @throws Exception
*/
public int exeDelete(PreparedStatement pstm) throws Exception{
if(con==null){
DataBaseConnection();
}
return pstm.executeUpdate();
}
/**
* 数据库连接关闭
* @return void
*/
public void closeDB() throws Exception{
if(stm!=null){
stm.close();
}
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}
/**
* 数据库接连
* @return void
*/
private void DataBaseConnection(){
CLASSFORNAME="com.mysql.jdbc.Driver";
SERVANDDB= "jdbc:mysql://localhost:3306/thizcaiwu?useUnicode=true&characterEncoding=utf8";
USER="root";
PWD="123456";
try{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB,USER,PWD);
stm=con.createStatement();
}
catch(Exception e){
System.out.println("Connection Error!!");
}
}
// public HashMap selectFromDB(){
// HashMap map = new HashMap();
// map.get("username");
// map.get("heigh");
// return null;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -