package ep1;
import java.sql.*;
public class openDB {
private String DatabaseDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String databaseConnStr= "jdbc:odbc:tt";
private String username = null;
private String password = null;
public Statement stmt = null;
public Connection conn = null;
public ResultSet rs = null;
public void setDatabaseConnStr(String databaseConnStr) {
this.databaseConnStr = databaseConnStr;
}
public String getDatabaseConnStr() {
return databaseConnStr;
}
public String getDatabaseDriver() {
return DatabaseDriver;
}
public void setDatabaseDriver(String databaseDriver) {
DatabaseDriver = databaseDriver;
}
// ----------------------------------------------------------
// 构造函数
// -----------------------------------------------------------
openDB() {
try {
Class.forName(this.DatabaseDriver);
this.username = "";
this.password = "";
} catch (ClassNotFoundException e) {
System.err.println("加载驱动器有错误:" + e.getMessage());
System.out.println("执行插入有错误:" + e.getMessage());
}
}
// ---------------------------------------------------------
// 执行数据插入
// ---------------------------------------------------------
public int executeInsert(String sql) {
int num = 0;
try {
conn = DriverManager.getConnection(this.databaseConnStr,username,
password);
stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
} catch (SQLException e) {
System.err.println("执行插入有错误:" + e.getMessage());
System.out.println("执行插入有错误:" + e.getMessage());
}
this.Close();
return num;
}
// ----------------------------------------------------------
// 执行删除数据
// ---------------------------------------------------------
public int ExecuteDelete(String sql) {
int num = 0;
try {
conn = DriverManager.getConnection(this.databaseConnStr,username,
password);
stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
} catch (SQLException e) {
System.err.println("执行删除有错误:" + e.getMessage());
System.out.println("执行删除有错误:" + e.getMessage());
}
this.Close();
return num;
}
// ----------------------------------------------------------
// 执行修改数据
// ----------------------------------------------------------
public int ExecuteUpdate(String sql) {
int num = 0;
try {
conn = DriverManager.getConnection(this.databaseConnStr,username,
password);
stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
} catch (SQLException e) {
System.err.println("执行修改有错误:" + e.getMessage());
System.out.println("执行修改有错误:" + e.getMessage());
}
this.Close();
return num;
}
// ---------------------------------------------------------
// 执行查询数据
// ---------------------------------------------------------
public ResultSet ExecuteQuery(String sql) {
try {
conn = DriverManager.getConnection(this.databaseConnStr,username,
password);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.err.println("执行查询有错误:" + e.getMessage());
System.out.println("执行查询有错误:" + e.getMessage());
}
// this.Close();
return rs;
}
// ----------------------------------------------------------
// 关闭数据库
// ----------------------------------------------------------
public void Close() {
try {
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception end) {
System.err.println("执行关闭Connection对象有错误:" + end.getMessage());
System.out.println("执行执行关闭Connection对象有错误:有错误:" + end.getMessage()); // 输出到客户端
}
}
// ----------------------------------------------------------
// 主函数,供测试使用
// ----------------------------------------------------------
public static void main(String[] args) {
openDB login = new openDB();
String sql = "insert into login values('w','w','1')";
int n;
n = login.executeInsert(sql);
System.out.println(n);
}
}
阅读(1121) | 评论(0) | 转发(0) |