📄 dbconnect.java
字号:
/**
********************************
* NTsky新闻发布系统V 1.0 *
* 功能:JDBC数据模板
* 代码编写:姚君林
* 编写日期:2003/4/1
* 代码修改:
* 修改日期:
*******************************
*/
package ntsky.database;
import java.sql.*;
import java.io.*;
public class DBConnect{
/**
*定义参数
*/
private String sDBDriver = "org.gjt.mm.mysql.Driver";//驱动程序
private String sConnStr = "jdbc:mysql://localhost:3306/nt?user=skyyjl&password=abc";//链接数据库
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private ResultSet rs = null;
/**
*statement(更新和查询)
*/
public DBConnect(){
try{
Class.forName(sDBDriver);//创建驱动
conn=DriverManager.getConnection(sConnStr);//加载连接
}
catch(Exception e){
System.out.println("DBConnect():"+e.getMessage());
}
}
//更新
public void executeUpdate(String sql) throws Exception{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
try{
Statement stmt=conn.createStatement();//建立访问数据库的对象
stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
//查询
public ResultSet executeQuery(String sql) throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
}
/**
* PrepareStatement(更新和查询)
*/
public DBConnect(String sql) throws Exception{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr);
this.prepareStatement(sql);
}
public Connection getConnection(){
return conn;
}
//创建访问数据库的对象
public void prepareStatement(String sql) throws Exception{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
prepstmt = conn.prepareStatement(sql);//?注意和stmt的区别
}
//设置参数(字符串)
public void setString(int index,String value) throws Exception{
value=new String(value.getBytes("GBK"),"ISO8859_1");
prepstmt.setString(index,value);
}
//(整数)
public void setInt(int index,int value) throws Exception{
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws Exception {
prepstmt.setBoolean(index,value);
}
//(时间)
public void setDate(int index,Date value) throws Exception {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws Exception {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws Exception {
prepstmt.setFloat(index,value);
}
public void setBinaryStream(int index,InputStream in,int length) throws Exception {
prepstmt.setBinaryStream(index,in,length);
}
public void clearParameters() throws Exception{
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement(){
return prepstmt;
}
//更新
public void executeUpdate() throws Exception {
if (prepstmt != null){
prepstmt.executeUpdate();
}
}
//查询
public ResultSet executeQuery() throws Exception {
if (prepstmt != null) {
rs=prepstmt.executeQuery();
return rs;
}
else return null;
}
/**
* 关闭数据库
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null){
prepstmt.close();
prepstmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -