/**
* Connect different databases by changing configuration files
*/
PackageCom.xykj.jdbc;Import Staticorg.junit.assert.*;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.Driver;Importjava.util.Properties;Importorg.junit.Test; Public classJDBCTest0 { PublicConnection getconnection ()throwsexception{String Driverclass=NULL; String Jdbcurl=NULL; String User=NULL; String Password=NULL; InputStream in= GetClass (). getClassLoader (). getResourceAsStream ("Jdbc.properties"); Properties Properties=NewProperties (); Properties.load (in); Driverclass= Properties.getproperty ("Driver"); Jdbcurl= Properties.getproperty ("Jdbcurl"); User= Properties.getproperty ("User"); Password= Properties.getproperty ("Password"); Driver Driver=(Driver) class.forname (Driverclass). newinstance (); Properties Info=NewProperties (); Info.put ("User", user); Info.put ("Password", password); Connection Connection=Driver.connect (Jdbcurl, info); returnconnection; } @Test Public voidTestgetconnection ()throwsexception{System.out.println (getconnection ()); }}
/*** JDBC Connection to Oracle database **/
1 PackageCom.xykj.jdbc;2 3 Import Staticorg.junit.assert.*;4 ImportJava.sql.*;5 Importjava.util.Properties;6 7 Importorg.junit.Test;8 9 Public classJdbctest {Ten One /** A * Driver is an interface: The database vendor must provide an implementation interface from which the database connection can be obtained. - * 1. Join the Oracle Driver - * 1> New lib directory, copy and paste jar package into lib. the * 2> Right-click Jar Package, build Path,add is added to the classpath. - */ - @Test - Public voidTestdriver () { +ResultSet res=NULL;//Create a result set object -PreparedStatement pre =NULL;//Create precompiled statement objects, usually with this instead of statement +Connection Connection =NULL;//Create a connection to a database A Try at { - - //1. Create an object of the driver implementation class -Driver Driver =NewOracle.jdbc.driver.OracleDriver ();//load the Oracle driver - - //2. Basic information about preparing to connect to a database: Url,user,password inString url = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; -Properties info =NewProperties (); toInfo.put ("User", "system"); +Info.put ("password", "SYS"); - the //3. Call Connect (url,info) of the driver interface to get the database connection *Connection = driver.connect (URL, info);//Get Connections $ System.out.println (connection);Panax NotoginsengSYSTEM.OUT.PRINTLN ("Database connection succeeded! "); - the //4. Working with the database +String sql = "SELECT * from Stu where Name =?";//precompiled statements,? Delegate Parameters APre = connection.preparestatement (SQL);//Instantiating precompiled Statements thePre.setstring (1, "Zhang San");//The parameter is set, and the previous 1 indicates the index of the parameter, not the index of the column name in the table +res = Pre.executequery ();//Execute Query - while(Res.next ()) $ $System.out.println ("Name:" +res.getstring ("name")) -+ "Gender:" +res.getstring ("Sex")) -+ "Age:" +res.getstring ("ages"))); the } - Catch(Exception e)Wuyi { the e.printstacktrace (); - } Wu finally - { About Try $ { - //close each of the above objects individually, because if you do not close it, it will affect performance and consume resources - //Note the order of the closing, the last used first close - if(Res! =NULL) A res.close (); + if(Pre! =NULL) the pre.close (); - if(Connection! =NULL) $ connection.close (); theSYSTEM.OUT.PRINTLN ("The database connection is closed! "); the } the Catch(Exception e) { the e.printstacktrace (); - } in } the } the}
JDBC Connects to Oracle database