📄 test1.java
字号:
package com.prodb;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class Test1 {
private Statement stmt;
private ResultSet rs;
private static Connection con;
/***************************************************
*函数名称: getCon()<br>
*函数功能: 获取数据库连接<br>
*返回值: Connection<br>
*参数说明: 无<br>
*创建: 白伟明 2004年9月22日
****************************************************/
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Project");
return ds.getConnection();
}catch(SQLException e){
throw e;
}
catch(NamingException e){
throw e;
}
}
/***************************************************
*函数名称: getStmt()<br>
*函数功能: 获取数据库集合<br>
*返回值: Statement
* stmt:返回数据库集合不用于SELECT语句<br>
*参数说明: 无<br>
*创建: 白伟明 2004年9月22日
****************************************************/
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*函数名称: getStmtread()<br>
*函数功能: 获取数据库集合<br>
*返回值: Statement
* stmt:返回数据库集合用于SELECT语句<br>
*参数说明: 无<br>
*创建: 白伟明 2004年9月22日
****************************************************/
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*函数名称: getRs()<br>
*函数功能: 获取数据库表中数据<br>
*返回值: ResultSet
* rs:返回数据库表中数据<br>
*参数说明: String sql:为表名[+条件]<br>
*创建: 白伟明 2004年10月20日
****************************************************/
public ResultSet getRs(String sql){
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT * FROM "+sql);
return rs;
}catch(Exception e){
System.out.println("getRs");
System.out.println(e.getMessage());
}
return null;
}
/***************************************************
*函数名称: addData()<br>
*函数功能: 添加数据<br>
*返回值: 无<br>
*参数说明: 无<br>
*创建: 白伟明 2004年10月20日
****************************************************/
public void addData() throws Exception{
try{
con=Test1.getCon();
PreparedStatement pstmt=con.prepareStatement("INSERT INTO Test VALUES(?,?)");
pstmt.setInt(1,3);
pstmt.setString(2,"适然");
pstmt.execute();
con.close();
}catch(SQLException e){
throw e;
}
}
/***************************************************
*函数名称: delData()<br>
*函数功能: 删除数据<br>
*返回值: 无<br>
*参数说明: 无<br>
*创建: 白伟明 2004年10月20日
****************************************************/
public void delData() throws Exception{
try{
stmt=this.getStmt();
stmt.executeUpdate("DELETE FROM Test WHERE id=3");
this.close();
}catch(SQLException e){
throw e;
}
}
/***************************************************
*函数名称:close()<br>
*函数功能:关闭数据库连接<br>
*返回值: void<br>
*参数说明:无<br>
*最或修改:白伟明
* 2004年9月4日
****************************************************/
public void close(){
try{
if(rs!=null)rs.close();
}catch(Exception e){
}
try{
if(stmt!=null)stmt.close();
}catch(Exception e){
}
try{
if(con!=null)con.close();
}catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -