📄 dboperate.java
字号:
package bean;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBOperate {
private Connection conn;
public ResultSet result=null;
private static String jndi_name= "java:comp/env/jdbc/mandarin";
public DBOperate(){
}
public static synchronized Connection getConnection()
{
Connection conn=null;
DataSource ds = null;
Context ctx;
try {
ctx = new InitialContext();
ds = (DataSource)ctx.lookup(jndi_name);
conn = ds.getConnection();
} catch (Exception e) {
System.err.println(e.getMessage()+"连接失败!");
}
return conn;
}
/*获取查询结果*/
public ResultSet getResult(String query_statement,String[] param)
{
try
{
conn = getConnection();
PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
if (param!=null)
for(int i=0;i<param.length;i++)
select_stm.setString(i+1,param[i]);
result=select_stm.executeQuery();
}catch(Exception e){
System.err.println(e.getMessage()+"没有获得结果集");
}
return result;
}
/*对数据库进行增加记录操作*/
public void insertRecord(Connection conn,String query_statement,String[] param) throws SQLException,java.io.UnsupportedEncodingException
{
try{
conn=getConnection();
PreparedStatement insert_stm=conn.prepareStatement(query_statement);
if (param!=null)
for(int i=0;i<param.length;i++)
insert_stm.setString(i+1,param[i]);
insert_stm.executeUpdate();
insert_stm.close();
conn.commit();
}
catch(Exception e)
{
System.err.println(e.getMessage()+"插入数据失败");
conn.rollback();
}finally{
closeConnection();
}
}
/*对数据记录进行更改操作*/
public void updateRecord(String query_statement,String[] param) throws SQLException,java.io.UnsupportedEncodingException
{
try{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
update_stm.setString(i+1,param[i]);
update_stm.executeUpdate();
update_stm.close();
conn.commit();
}catch(Exception e){
System.err.println(e.getMessage()+"更新数据失败!");
conn.rollback();
}finally{
closeConnection();
}
}
/*删除数据记录*/
public void deleteRecord(String query_statement,String[] param) throws SQLException,java.io.UnsupportedEncodingException
{
try
{
conn=getConnection();
PreparedStatement delete_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
delete_stm.setString(i+1,param[i]);
delete_stm.executeUpdate();
delete_stm.close();
conn.commit();
}catch(Exception e){
System.err.println(e.getMessage());
conn.rollback();
}finally{
closeConnection();
}
}
public boolean setDataInfo(String sql) {
try {
conn =getConnection();
Statement stmt = null;
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
closeConnection();
}
}
public ResultSet getDataInfo(String sql){
Statement stmt = null;
try{
conn =getConnection();
stmt = conn.createStatement();
result = stmt.executeQuery(sql);
}catch (Exception e){
System.err.println(e.getMessage()+"没有获得结果集!");
}
return result;
}
public void closeConnection() {
try {
conn.close();
conn=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -