DB. Java
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Public class dB {
Private string sdbdriver = "oracle. JDBC. Driver. oracledriver ";
// Private Static string url = "JDBC: oracle: thin: @ 172.18.62.60: 1521: orcl ";
Private Static string url = "JDBC: oracle: thin: @ localhost: 1521: orcl ";
Private Static string user = "resmg ";
Private Static string Pwd = "resmg ";
Private Static connection con = NULL;
Private Static statement stmt = NULL;
Public dB (){
Try {
Class. forname (sdbdriver );
Getcon ();
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
}
Public static connection getcon (){
If (con = NULL ){
Try {
Con = drivermanager. getconnection (URL, user, PWD );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return con;
}
Public static statement getstmt (){
If (stmt = NULL ){
Try {
Stmt = con. createstatement ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return stmt;
}
}
Dbdao. Java
Package com. Services. Mon. log;
Import java. SQL. connection;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. Map;
Public class dbdao extends dB {
Private statement stmt;
Public Boolean insert (string table, hashmap <string, string> map,
Connection con ){
Boolean flag = false;
Iterator iter = map. entryset (). iterator ();
String skey = "";
String sval = "";
While (ITER. hasnext ()){
Map. Entry entry = (Map. Entry) ITER. Next ();
Object key = entry. getkey ();
Object val = entry. getvalue ();
If (ITER. hasnext ()){
Skey + = Key + ",";
Sval + = "'" + Val + "',";
}
If (! ITER. hasnext ()){
Skey + = (string) Key;
Sval + = "'" + Val + "'";
}
}
String SQL = "insert into" + Table + "(" + skey + ") values (" + sval
+ ")";
System. Out. println (SQL );
Try {
Stmt = dB. getstmt ();
Stmt.exe cuteupdate (SQL );
Flag = true;
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return flag;
}
Public resultset query (string SQL, connection con, Boolean flags ){
Resultset rs = NULL;
Try {
Stmt = dB. getstmt ();
Rs = stmt.exe cutequery (SQL );
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return Rs;
}
Public Boolean Update (string table, string condition,
Hashmap <string, string> map, connection con ){
Boolean flag = false;
Iterator iter = map. entryset (). iterator ();
String SETC = "";
While (ITER. hasnext ()){
Map. Entry entry = (Map. Entry) ITER. Next ();
Object key = entry. getkey ();
Object val = entry. getvalue ();
If (ITER. hasnext ())
SETC + = Key + "= '" + Val + "',";
If (! ITER. hasnext ())
SETC + = Key + "= '" + Val + "'";
}
String SQL = "Update" + Table + "set" + SETC + "where" + condition;
System. Out. println (SQL );
Try {
Stmt = dB. getstmt ();
Flag = stmt.exe cuteupdate (SQL)> 0? True: false;
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return flag;
}
Public static void main (string [] AGRs ){
Dbdao DB = new dbdao ();
String SQL = "select count (*) from tmon_catalog_log_info ";
Resultset rs = dB. Query (SQL, DB. getcon (), false );
Try {
While (Rs. Next ()){
System. Out. println (Rs. getstring (1 ));
}
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}