Tools class for Java linked database

Source: Internet
Author: User
link | data | database package spider.tools;

Import java.sql.*;

public class DBTools {

public static final int ORACLE = 0;
public static final int SQL Server = 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 = SQL Server) {
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;
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.