<>1,DBUtil class
package tools; import java.sql.Connection; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.SQLException; /** *
JDBC connect Oracle Sample code for database * @author:yunfan * */ public class DBUtil{ static { try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();// load Oracle Driver
System.out.println("oracle Driver loading !"); } catch(InstantiationException e1) {
System.out.println(" Instance exception "); } catch(IllegalAccessException e2) {
System.out.println(" Access exception "); } catch(ClassNotFoundException e3) {
System.out.println("MySQL Driver class not found "); } } /*** * Returns a database connection */ public static
Connection getConnection() { Connection connection = null;// Create a database connection try {
System.out.println(" Start trying to connect to the database !"); String url =
"jdbc:oracle:thin:@127.0.0.1:1521:oracle";//Oracle Default database name for String user =
"system";// System default user name String password = "system";// Password set during installation connection =
DriverManager.getConnection(url, user, password);// Get connection
System.out.println(url); System.out.println(" user name :"+user+"\t"+" password :******");
System.out.println(" Database connection successful !"); return connection; } catch (Exception e) {
e.printStackTrace(); return null; } } }
<>2,ConnectTest class
package tools; import java.sql.Connection; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.SQLException; public class
ConnectTest { public static void main(String[] args) { Connection connection =
null; PreparedStatement ps = null; ResultSet result = null; try { connection =
DBUtil.getConnection(); String sql = "select * from user where id = ?"; ps =
connection.prepareStatement(sql); ps.setInt(1, 1); result = ps.executeQuery();
while (result.next()) { System.out.println(result.getInt("id") + " user name :" +
result.getString("name")); } } catch (SQLException e) { e.printStackTrace(); }
finally { try { // Close the above objects one by one , Because if it is not turned off, it will affect the performance , And it takes up resources // Pay attention to the closing sequence , Last used first closed if
(result != null) { result.close(); } if (ps != null) { ps.close(); } if
(connection != null) { connection.close(); } System.out.println(" Database connection closed !"); }
catch (Exception e) { e.printStackTrace(); } } } }
<> console output :
oracle Driver loading ! Start trying to connect to the database ! jdbc:oracle:thin:@127.0.0.1:1521:oracle user name :system
password :****** Database connection successful ! 1 user name :yunfan Database connection closed !

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network