To use this class, use the jar package to connect to Oracle.
Steps:
1. Load the driver
2. Establish a connection
3. Obtain the SQL statement object (statement)
4. Execute SQL statements
5. release resources
Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement;/** @ author sunnycoffee * @ time 2011-9-16 * todo tool class used to connect to the database */public class dbconnection {private connection conn; private statement Stat; private string URL; private string user; private string password; // obtains the statement object public dbconnection () {try {class. forname ("oracle. JDBC. driver. oracledriver "); // load the driver url =" JDBC: oracle: thin: @ localhost: 1521: orcl "; user =" system "; // database Username Password = "myoracle"; // Database Password conn = drivermanager. getconnection (URL, user, password); // get the connection stat = Conn. createstatement (); // get statement} catch (classnotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}// insert, modify, and delete interfaces: Public int executeupdate (string SQL) {int flag = 0; try {flag = stat.exe cuteupdate (SQL ); // Execute SQL statement} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {stat. close (); // release the resource Conn. close ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return flag;} // The Public resultset executequery (string SQL) {resultset set = NULL; try {set = stat.exe cutequery (SQL );} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {// try {// STAT. close (); // Conn. close (); //} catch (sqlexception e) {// todo auto-generated Catch Block // E. printstacktrace (); //} return set ;}}
Url = "JDBC: oracle: thin: @ localhost: 1521: orcl"; meaning:
JDBC Java Database Connection
Oracle Database type to be connected
Thin client in thin connection mode
@ Localhost: 1521 IP address and port number of the database
Orcl Database Name
The query interface returns a set. If the connection is closed, the set. Next method is unavailable.