java Oracle database connection instructions

  In our tests, we often need to connect to Oracle database query comparison. Here, we look at how to use java code to connect to the database, and remove the value we want.

  First, java if you want to connect to the Oracle database, you need jdbc jar package. Download: https: //mvnrepository.com/artifact/ojdbc/ojdbc

  The embodiment using Oracle, Url used for the connection of the following two:

  Oracle URL:
  jdbc:oracle:thin:@HostName(or IP address):1521:SID
  jdbc:oracle:thin:@//HostName(or IP address):1521:SERVICENAME
  
  This parameter indicates thin compact drive, HostName (or IP address): 1521 This means that a particular network location generations Oracle database. SID / ServiceName This means that the service SID specific database or database link.
  Before actually connecting to the database, we need to register a drive.
  
 the try { 
  the Class.forName (
"oracle.jdbc.driver.OracleDriver" ); } the catch (a ClassNotFoundException E) { System.out.println ( "class not find the driver, the driving load failure!");

  This approach is to use a class loader registration drive.

  Of course, we can also use the drive to register new OracleDriver objects.

Driver driver = new OracleDriver();
DriverManager.deregisterDriver(driver);

  Both methods allow, can be selected according to actual situation.

  Next, we will get the link Oracle database.

connect = DriverManager.getConnection ( "jdbc: oracle: thin: @OracleDataBaseHost: 1521: XE", "connected to oracle database user name", "username and password");

  If you use a driver registered objects. You can use the following code

Properties properties = new Properties();
properties.put("user", "oracle数据库用户名");
properties.put("password", "用户名密码");
connect = driver.connect("jdbc:oracle:thin:@localhost:1521:XE", properties);

  With the following link, we can use this link to get the object to perform sql statement.

statement = connect.createStatement();

  If the driver is the way the object

PreparedStatement preState = connect.prepareStatement("select  * from tb1 where name = ?");

  Then again, it is the concrete implementation of sql statement

resultSet = statement.executeQuery("select * from tb1");

  If PrepareStatement object is the same.

ResultSet rs = preState.executeQuery();

  Then, the processing is performed in accordance with the results obtained ReslutSet.

while (rs.next()){
  int id = rs.getInt("id");
  String name = rs.getString("name");
  String wbsCode = rs.getString("wbscode");
  System.out.println(id+"   "+name+"   "+ wbsCode);  //打印输出结果集
}

  Finally, one by one close links to resources. Without closing words will affect performance, and resource-intensive. Note the order closed, the first closed last used!

try {
  if (rs!=null) rs.close();
  if (statement!=null) statement.close();
  if (connect!=null) connect.close();
} catch (SQLException e) {
   e.printStackTrace();
}

  Thus, the overall connection is completed and reading data from an Oracle database operation.

 

  
  

Guess you like

Origin www.cnblogs.com/generalli2019/p/11490151.html