Learning notes: A program connecting to the Oracle database for addition, deletion, modification, and query

Source: Internet
Author: User

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 ();
}
}

}

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.