java链接数据库的工具类
来源:互联网 发布:淘宝客鹊桥安全不 编辑:程序博客网 时间:2024/05/29 04:32
package spider.tools;
import java.sql.*;
public class DBTools {
public static final int ORACLE = 0;
public static final int SQLSERVER = 1;
private static String oracleDriver =
"oracle.jdbc.driver.OracleDriver";
private static String sqlserverDriver =
"com.microsoft.jdbc.sqlserver.SQLServerDriver";
public Connection createConnection(String host, String dataBase, String user, String password,
int dbKind) {
Connection result = null;
String dbUrl = null;
String dbDriver = null;
if (dbKind == ORACLE) {
dbUrl = createOracleUrl(host, dataBase);
dbDriver = oracleDriver;
}
else if (dbKind == SQLSERVER) {
dbUrl = createSqlserverUrl(host, dataBase);
dbDriver = sqlserverDriver;
}
else {
MyUtil.log(
"initial database connection failure: " + "Unsupport data base!");
return null;
}
try {
Class.forName(dbDriver);
result = DriverManager.getConnection(dbUrl, user, password);
}
catch (Exception e) {
MyUtil.log(
"initial database connection failure: " + e.toString());
return null;
}
return result;
}
public String createOracleUrl(String host, String dataBase) {
String oracleUrl = "jdbc:oracle:thin:@"
+ host + ":1521:"
+ dataBase;
return oracleUrl;
}
public String createSqlserverUrl(String host, String dataBase) {
String sqlServerUrl = "jdbc:microsoft:sqlserver://"
+ host + ":1433;DatabaseName="
+ dataBase;
return sqlServerUrl;
}
public boolean isTableExist(Connection cnn, String table) {
boolean result = false;
String strSQL = "select * from sysobjects where id=object_id('" + table + "')";
Statement st = null;
ResultSet rs = null;
try {
st = cnn.createStatement();
rs = st.executeQuery(strSQL);
if(rs.next())
result = true;
}
catch (SQLException ex) {
MyUtil.log("Error happen when check table existance! table name: " + table);
}
finally {
if (st != null) {
try {
st.close();
}
catch (SQLException ex1) {}
}
if (rs != null) {
try {
rs.close();
}
catch (SQLException ex1) {}
}
}
return result;
}
public boolean clearTable(Connection cnn, String table) {
if (!isTableExist(cnn, table))
return false;
boolean flag = true;
PreparedStatement ps = null;
try {
ps = cnn.prepareStatement("delete from " + table);
ps.executeUpdate();
}
catch (Exception e) {
flag = false;
}
finally {
if (ps != null) {
try {
ps.close();
}
catch (SQLException ex) {}
}
}
return flag;
}
}
- java链接数据库的工具类
- java通过配置文件jdbc.properties链接Oracle数据库工具类
- 数据库链接工具类--1
- jdbc 链接数据库工具类
- java链接数据库的应用类
- java 链接mysql 链接数据库的步骤
- Java链接Access工具类
- 各种数据库的java链接
- java与数据库的链接
- java的数据库链接1
- MySql,SqlServer,Oracle链接数据库工具类
- jdbc数据库链接之抽取工具类
- Mysqli扩展链接数据库工具类
- 数据库链接使用工具
- 简单的Java数据库关闭工具类
- java连接数据库的工具类
- java操作数据库的工具类
- java连接数据库的工具类
- ZT: Delphi编程规范1.0.0.0 (yckxzjj )
- ZT:Delphi源程序格式书写规范
- 请帮忙?
- RS6000小型机,可以远程供大家练手--收藏
- 探察RUNTIME_CLASS之类的秘密
- java链接数据库的工具类
- 一个站长对黑客的血泪控诉
- 国际上负责音视频编码标准化的组织有哪些?
- 视频编码新标准H.264中抗误码技术的研究
- 视频编码标准的发展:从H.261到H.264
- 如何用java语言控制短信设备实现短信收发?
- 动态连接数据库及动态建立ODBC,DSN(ZT)
- 下一代压缩视频标准性能及其技术特征
- VC++编程实现网络嗅探器