JDBC connection to Oracle Database

Source: Internet
Author: User

Package com. hhwy. test;

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. SQL. connection;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. SQL. drivermanager;

Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. SQL. datasource;

Import org. Apache. commons. DBCP. basicdatasource;

 

Public class dbtools
{
Private connection conn = NULL;
Private resultset rs = NULL;
Private statement stmt = NULL;
Private string temppath = "Temp"; // location where the Blob temporary file is stored
Private Static final basicdatasource;
Static {
Datasource = new basicdatasource ();
Datasource. setdriverclassname ("oracle. JDBC. Driver. oracledriver ");
Datasource. seturl ("JDBC: oracle: thin: @ 192.168.0.90: 1521: bjspdgis ");
Datasource. setusername ("epmmanager ");
Datasource. setpassword ("manager ");
}
 
Public dbtools ()
{
System. Out. println ("--------------------- dbtools enter ----");
Try {
// Conn = This. getconnection ();
// Conn = This. createconnection ();
Conn = dbtools. getdbpoolconnection ("JDBC/dbpool ");
If (Conn! = NULL)
{
This. stmt = conn. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable );
}
} Catch (exception E)
{
E. printstacktrace ();

}

}
// Tomcat Connection Pool Mode S
Private Static connection getdbpoolconnection (string poolname)
{
Context c_cont = NULL;
Datasource DS = NULL;
Try
{
C_cont = new initialcontext ();
} Catch (exception E)
{
System. Out. println ("couldn't build an initial context:" + E );
Return NULL;
}
Try
{
DS = (datasource) c_cont.lookup ("Java:/COMP/ENV/" + poolname );
} Catch (exception E)
{
E. printstacktrace ();
Return NULL;
}
Try
{
Return Ds. getconnection ();
} Catch (sqlexception E)
{
E. printstacktrace ();
System. Out. println ("sqlexception in databean. getpoolconnection ():"
+ E. getmessage () + "JNDI is:" + poolname );
Return NULL;
}
}
 
// JDBC connection
Public connection createconnection ()
{
Try
{
// Dbpool DBP = new dbpool ();
Connection con = NULL;
String connname = "JDBC: oracle: thin: @ 192.168.0.90: 1521: bjspdgis ";
// System. Out. Print ("connect to getconnect ():" + connname + "...");
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Con = drivermanager. getconnection (connname, "epmmanager ",
"Manager ");

If (con! = NULL)
{
System. Out. println ("------------- connection successful !!! ");
} Else {

System. Out. println ("---------------- Connection Failed !!! ");
}
Return con;
} Catch (exception E)
{
E. printstacktrace ();
}
Return NULL;

}
 
Public connection getconnection ()
{
Connection conn1 = NULL;
Try {
Conn1 = datasource. getconnection ();
} Catch (exception e ){
E. printstacktrace ();
}

Return conn1;

}
 
Public resultset executequery (string SQL) throws sqlexception {
 
Try
{
System. Out. println (SQL );
Rs = stmt.exe cutequery (SQL );
} Catch (exception E)
{
E. printstacktrace ();
}

Return Rs;
}
Public void closedb ()
{
Try
{
Datasource. Close ();
} Catch (exception E)
{
E. printstacktrace ();
}

}

Public static void shutdowndbcp () throws exception {
System. Out. println ("Disconnect datasource. Close ()");
Datasource. Close ();


}
Public void closecon ()
{
Try
{
Conn. Close ();
} Catch (sqlexception E)
{
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
 
 

Related Article

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.