📄 web158_com_server_dbconn.java
字号:
package web158.com;
/**
* @param 李建东
*
* 联系电话:0898-62925341
*
* 联系QQ:813751 657597 8912740
*
* 网 址:
* http://www.web156.com
* http://www.web158.com
*/
import java.sql.*;
import java.awt.*;
import javax.swing.*;
class Web158_Com_Server_DBConn{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=web158_com";
String user="sa";
String password="";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public Web158_Com_Server_DBConn ()
{
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println( e.getMessage());
JOptionPane.showMessageDialog(null, "数据库驱动加载错误,请检查!");
Close();
System.exit(0);
}
}
// 返回数据库连接对象
public Connection Creatconn(){
conn = null;
try{
conn=DriverManager.getConnection(getConfig(),user,password);
return conn;
}
catch(Exception fe){
System.err.println("连接数据库 " + fe.getMessage());
//JOptionPane.showMessageDialog(null, "服务器连接失败");
JOptionPane.showMessageDialog(null, "连接数据库失败,请检查!");
Close();
System.exit(0);
return null;
}
}
// 返回状态集对象
public Statement CreatStat(){
stmt = null;
try{
conn=this.Creatconn();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
return stmt;
}
catch(Exception fe){
System.err.println("返回记录集 " + fe.getMessage());
//JOptionPane.showMessageDialog(null, "返回状态集失败");
JOptionPane.showMessageDialog(null, "返回状态集时失败,请检查!");
Close();
return null;
}
}
// 返回记录集对象
public ResultSet getResult(String sql){
rs = null;
try{
stmt = this.CreatStat();
rs = stmt.executeQuery(sql);
return rs;
}
catch(SQLException ex){
System.err.println("getResult: " + ex.getMessage());
//JOptionPane.showMessageDialog(null, "返回记录集失败");
JOptionPane.showMessageDialog(null, "返回记录集失败,请检查!");
Close();
return null;
}
}
//执行更新 删除语句
public boolean executeSql(String sql)
{
try{
stmt=this.CreatStat();
stmt.executeUpdate(sql);
conn.commit();
return true;
}
catch(Exception e)
{
System.err.println("exectueSql: "+e.toString());
//JOptionPane.showMessageDialog(null, "数据更新失败");
JOptionPane.showMessageDialog(null, "更新数据库失败,请检查!");
Close();
return false;
}
}
public void Close()
{
try
{
rs.close();
stmt.close();
conn.close();
}
catch(Exception ef)
{}
}
public String getConfig()
{
return sConnStr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -