Package Tj.test.demo;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
public class Orcaleconnectiontest {
public static void Testoracle ()
{
Connection con = null;//Create a database connection
PreparedStatement pre = null;//creates a precompiled statement object, which is usually used without statement
ResultSet result = null;//Create a results set object
Try
{
Class.forName ("Oracle.jdbc.driver.OracleDriver");//Load Oracle Driver
System.out.println ("Start trying to connect to the database!") ");
String url = "jdbc:oracle:" + "THIN:@192.168.5.162:1521:ORCL";//127.0.0.1 is a native address, XE is the default database name for Lite Oracle
String user = "WSDC";//user name, system default account name
String password = "WSDC";//the password you choose to set when you install
con = drivermanager.getconnection (url, user, password);//Get connection
SYSTEM.OUT.PRINTLN ("Connection Successful! ");
String sql = "SELECT * from T_ZF_JCJ where jjbh=?"; /precompiled statement, "? The delegate parameter
Pre = con.preparestatement (SQL);//instantiation of precompiled statements
Pre.setstring (1, "J11022441000020151201531");//Set the parameter, the preceding 1 indicates the index of the parameter, not the index of the column name in the table
result = Pre.executequery ();//Execute query, note that no additional arguments are required in parentheses
while (Result.next ())
When the result set is not empty
System.out.println (result.getstring ("JJBH"));
}
catch (Exception e)
{
E.printstacktrace ();
}
Finally
{
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 (result! = NULL)
Result.close ();
if (pre! = null)
Pre.close ();
if (con! = null)
Con.close ();
SYSTEM.OUT.PRINTLN ("The database connection is closed! ");
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
public static void Main (string[] args) {
Testoracle ();
}
}
Native Orcale database connection