📄 dbconnect.java
字号:
package cn.register.db;
import java.sql.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBConnect {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rst;
private String str1; //创建数据库的一个Connection连接
private void init(){
try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
catch(Exception e) {
e.printStackTrace();
}
}
public DBConnect(){ //构造函数
try{
init(); //获得一个数据库连接
stmt = conn.createStatement();
}
catch(Exception e) {e.printStackTrace();}
}
//执行数据库查询语句,s为sql语句,把查询结果赋给ResultSet
public void excuteQuery(String s) {
try{
if(stmt != null) {
rst = stmt.executeQuery(s);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
//对数据库进行update操作
public int excuteUpdate(String s) {
int status = 0;
try{
if(stmt != null)
status = stmt.executeUpdate(s);
}
catch(Exception e) {
e.printStackTrace();
}
return status;
}
//以下为赋值方法
public void setString(int i,String s) { //字符串赋值
try { pstmt.setString(i,s); }
catch(Exception e) { e.printStackTrace(); }
}
public void setBoolean(int i, boolean flag) { //布尔型赋值
try{ pstmt.setBoolean(i, flag);}
catch(Exception e) { e.printStackTrace();}
}
public void setDate(int i, Date date) { //日期类型赋值
try{pstmt.setDate(i, date);}
catch(Exception e) { e.printStackTrace();}
}
public void setTime(int i, Time time) { //时间类型赋值
try{pstmt.setTime(i,time);}
catch(Exception e) { e.printStackTrace();}
}
public void setShort(int i, short word0) { //Short类型赋值
try{pstmt.setShort(i,word0);}
catch(Exception e) {e.printStackTrace();}
}
public void setInt(int i, int j) { //整数类型赋值
try{pstmt.setInt(i,j);}
catch(Exception e) { e.printStackTrace();}
}
public void setLong(int i, long l) { //长整型赋值
try{pstmt.setLong(i, l);}
catch(Exception e) { e.printStackTrace();}
}
public void setFloat(int i, float f) { //浮点型赋值
try{pstmt.setFloat(i, f);}
catch(Exception e) { e.printStackTrace();}
}
public void setDouble(int i, double d) { //Double类型赋值
try{pstmt.setDouble(i, d);}
catch(Exception e) {e.printStackTrace();}
}
//以下为取值方法
public boolean getBoolean(int i) throws Exception{ //取得布尔类型的字段,通过列号
return rst.getBoolean(i);
}
public boolean getBoolean(String s) throws Exception{ //取得布尔类型的字段,通过字段名
return rst.getBoolean(s);
}
public Date getDate(int i) throws Exception{ //取得Date类型的字段,通过列号
return rst.getDate(i);
}
public Date getDate(String s) throws Exception{ //取得Date类型的字段,通过字段名
return rst.getDate(s);
}
public Time getTime(int i) throws Exception{ //取得Time类型的字段,通过列号
return rst.getTime(i);
}
public Time getTime(String s) throws Exception{ //取得Time类型的字段,通过字段名
return rst.getTime(s);
}
public double getDouble(int i) throws Exception{ //取得Double类型的字段,通过列号
return rst.getDouble(i);
}
public double getDouble(String s) throws Exception{ //取得Double类型的字段,通过字段名
return rst.getDouble(s);
}
public float getFloat(int i) throws Exception{ //取得Float类型的字段,通过列号
return rst.getFloat(i);
}
public float getFloat(String s) throws Exception{ //取得Float类型的字段,通过字段名
return rst.getFloat(s);
}
public int getInt(int i) throws Exception{ //取得整数类型的字段,通过列号
return rst.getInt(i);
}
public int getInt(String s) throws Exception{ //取得整数类型的字段,通过字段名
return rst.getInt(s);
}
public long getLong(int i) throws Exception{ //取得长整型的字段,通过列号
return rst.getLong(i);
}
public long getLong(String s) throws Exception{ //取得长整型的字段,通过字段名
return rst.getLong(s);
}
public short getShort(int i) throws Exception{ //取得Short类型的字段,通过列号
return rst.getShort(i);
}
public short getShort(String s) throws Exception{ //取得Short类型的字段,通过字段名
return rst.getShort(s);
}
public String getString(int i) throws Exception{ //取得字符串类型的字段,通过列号
return rst.getString(i);
}
public String getString(String s) throws Exception { //取得字符串类型的字段,通过字段名
return rst.getString(s);
}
//指针下移一位
public boolean next(){
try {return rst.next();}
catch(Exception e) {
e.printStackTrace();
return false;
}
}
//释放内容
public void close(){
try{
if(conn !=null) conn.close();
if(stmt !=null) stmt.close();
if(rst != null) rst.close();
}
catch(Exception e) {e.printStackTrace();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -