📄 connectbean.java
字号:
package com.zte.im.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import com.zte.im.bo.UserOperation;
import com.zte.im.vo.DepartInfo;
import com.zte.im.vo.UserInfo;
/**
* 本程序用于数据库查询及更新操作。
* 查询及更新方法皆为抽象出来的一般形式,
* 只需传入sql语句和表字段即可。
* 其中: display方法用于查询操作;
* update方法用于更新操作。
*
* @author 王波
* @since 2007年3月2日
* @version 1.3
* test vss---Just change this place
* 联系方式:MSN:wave_1102@hotmail.com ; QQ:56915469
*/
public class ConnectBean{
//-------------------------------------------------------Class Variable
private Connection con;
//-------------------------------------------------------Methods
/**
* 本方法用于数据库查询。
* 只需要传入查询语句及查询字段即可实现数据查询。
*
* @param sql: 执行查询操作的SQL语句。
* @param fields: 要查询的sql字段。
* @return ArrayList 查询操作返回的数据。
*/
public ArrayList display(String sql, String[] fields){
ArrayList al = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
//如果打开连接失败,则给出提示。
if(!openConnection()){
System.out.println("Connection failure!!!");
return null;
}
//System.out.println("数据库连接成功!!!");
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
al = new ArrayList();
while(rs.next()){
//将一个对象的记录放入HashMap。
HashMap map = new HashMap();
for(int i = 0; i < fields.length; i++){
map.put(fields[i],rs.getString(fields[i]));
}
// System.out.println("map="+map);
//将包含记录的每个HashMap对象放入ArrayList.
al.add(map);
}
}catch (SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e2){
e2.printStackTrace();
}finally {
//关闭数据库。
try {
rs.close();
ps.close();
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
return al;
}
/**
* 本方法用于数据库更新操作。
* 包括:增加、删除、修改数据库数据操作。
*
* @param sql 执行更新操作的sql语句。
* @return 更新操作返回的行数。
*/
public int update(String sql){
int lineAffected = 0; //更新操作影响的行数.
PreparedStatement ps = null;
try {
//如果打开数据库连接失败,则给出提示信息。
if(!openConnection()){
System.out.println("Connection failure...");
return 0;
}
//执行更新操作。
ps = con.prepareStatement(sql);
lineAffected = ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e2){
e2.printStackTrace();
}finally {
//关闭数据库。
try{
ps.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return lineAffected;
}
public void insert(String string){ //数据插入数据库
PreparedStatement ps=null;
ResultSet rs =null;
String sql=string;
try{
if(!openConnection()){
System.out.println("Connection failure!!!");
}
ps=con.prepareStatement(sql);
ps.executeUpdate();
con.close();
} catch(SQLException e){
e.printStackTrace();
}
catch(Exception e){
System.out.println(e);
}
}
/**
* 测试数据库连接是否成功。
*
* @param args
*/
public static void main(String[] args){
// String sql = "select * from student";
// String[] fields = {"id","name","gender","age"};
// long startTime = System.currentTimeMillis();
// ConnectBean cb = new ConnectBean();
// ArrayList al = cb.display(sql,fields);
// System.out.println(al);
// long endTime = System.currentTimeMillis();
// //输出程序运行时间。
// System.out.println("程序运行时间:" + (endTime - startTime)+"毫秒。");
System.out.println("-----部门列表-----");
UserOperation uo = new UserOperation();
DepartInfo[] depart = uo.getDepart();
for (int i =0; i < depart.length; i++){
System.out.println(depart[i].getDepartId()+"\t"+depart[i].getDepartName());
}
System.out.println("-----编号为01部门的员工列表-----");
String departId = "01";
UserInfo[] employee = uo.getEmpByDepart(departId);
for (int i =0; i < employee.length; i++){
System.out.println(employee[i].getEmpId()+"\t"+employee[i].getEmpName());
}
String sql = "select senderid,message,msgtime from MessageInfo where "
+"(receiverid='0001' or senderid='0001') and isviewed<>0 order by msgtime asc";
String[] fields = {"senderid","message","msgtime"};
System.out.println(sql);
ConnectBean cb = new ConnectBean();
ArrayList al = cb.display(sql, fields);
System.out.println(al);
}
/**
* 打开数据库连接。
*
* @return 连接成功返回true;失败返回false。
* @throws ClassNotFoundException
* @throws SQLException
*/
private boolean openConnection() throws ClassNotFoundException,SQLException{
boolean boo=false;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.33:1521:message", "message", "message");
if (con != null) boo=true;
return boo;
}
/**
* 说明:这个是用sql server测试用的,如果想用oracle的,只需把上面的屏蔽去掉,
* 并把下面这部分屏蔽掉,不要删除。
*/
// private boolean openConnection() throws ClassNotFoundException,SQLException{
// boolean bl = false;
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// String protocol = "jdbc:microsoft:sqlserver://192.168.1.81:1433;DatabaseName=InstantMsg";
// con = DriverManager.getConnection(protocol, "sa", "");
// if(con != null) bl = true;
// return bl;
// }
/**
* 需要在JDeveloper环境下配置OracleJDBC数据源,具体可参考其他资料。
*/
// private boolean openConnection() throws ClassNotFoundException,SQLException,NamingException{
// boolean boo=false;
// InitialContext ic = new InitialContext();
// DataSource ds = (DataSource) ic.lookup("jdbc/JDBCConnectionCoreDS");
// con = ds.getConnection();
// if (con != null) boo=true;
// return boo;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -