📄 d0caa843b9bc001d1a0bdb0e07cbdb75
字号:
package org.dbUtil;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.RowSet;
import org.DAO.UserinfoDAO;
import sun.jdbc.rowset.CachedRowSet;
public class DBUtil {
public DBUtil() {
}
/**
* 添加
*
* @param SQL
* @return
* @throws SQLException
*/
public static Boolean exe(String sql) {
System.out.println("===" + sql);
boolean bl = false;
try {
Connection conn = DBConn.getConnection();
try {
Statement stmt = conn.createStatement();
bl = stmt.execute(sql);
stmt.close();
} catch (SQLException e) {
System.out.println("update:" + e.toString());
} finally {
DBConn.close(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return bl;
}
/**
* 查询
*
* @param SQL
* @return
* @throws SQLException
*/
public static CachedRowSet exeQuery(String sql) {
CachedRowSet crs = null;
try {
Connection conn = DBConn.getConnection();
// System.out.println("---------------------conn
// closed:"+conn.isClosed());
try {
System.out.println("sql-------------------" + sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
crs = new CachedRowSet();
crs.populate(rs);
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println("exeQuery:" + e.toString());
} finally {
DBConn.close(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return crs;
}
/**
* 更新
*
* @param sql
* @return
*/
public static boolean update(String sql) {
System.out.println("===" + sql);
boolean bl = false;
try {
Connection conn = DBConn.getConnection();
CachedRowSet crs = null;
try {
Statement stmt = conn.createStatement();
if (stmt.executeUpdate(sql) > 0)
bl = true;
stmt.close();
} catch (SQLException e) {
System.out.println("update:" + e.toString());
} finally {
DBConn.close(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return bl;
}
/**
* 判断是否有记录
*
* @param sql
* @return
*/
public static boolean hasRecord(String sql) {
boolean bl = false;
try {
RowSet rs = exeQuery(sql);
if (rs.next())
bl = true;
rs.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
}
return bl;
}
/**
* 查询记录
*
* @param SQL
* @return
* @throws SQLException
*/
public static List<UserinfoDAO> Query(String sql) {// <UserinfoDAO>就是我们的javabean类
ResultSet rs = null;
List list = null;
Connection conn = null;
try {
conn = DBConn.getConnection();
System.out.println("sql-------------------" + sql);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
// HashMap<String,String> set=new HashMap();
list = new ArrayList();
while (rs.next()) {
// 以下就是我们要修改的内容,上面的原封不动的copy
// 根据数据库字段进行修改
Integer no=Integer.valueOf(rs.getInt(1));
String id1 = String.valueOf(rs.getInt(2));
String name = rs.getString(3);
String street = rs.getString(4);
String city = rs.getString(5);
String pro = rs.getString(6);
String zip = rs.getString(7);
String tel = rs.getString(8);
Date bir = rs.getDate(9);
String sex = rs.getString(10);
String wel = rs.getString(11);
String earn = rs.getString(12);
String fun = rs.getString(13);
// new一个javabean对象
UserinfoDAO user = new UserinfoDAO();
// 对它的属性进行赋值
user.setNo(no);
user.setEmployId(id1);
user.setEmployName(name);
user.setEmployStreet(street);
user.setEmployCity(city);
user.setEmployPro(pro);
user.setEmployZip(zip);
user.setEmployTel(tel);
user.setEmployBir(bir);
user.setEmploySex(sex);
user.setEmployWel(wel);
user.setEmployEarn(earn);
user.setEmployFun(fun);
// 将javabean组装入list列表
list.add(user);
}
} catch (Exception e) {
System.out.println("exeQuery:" + e.toString());
} finally {
DBConn.close(conn);
}
return list;
}
public UserinfoDAO getUserinfoDAOById(String Id) {
List list = Query("select * from wid where did=" + Id);
if (list.size() > 0) {
return (UserinfoDAO) list.get(0);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -