Oracle19c 连接java 简单demo (仅数据库连接)

OJDBC 连接oracle 简短实例(请在运行时。。打开你的数据库)
创建一个maven项目。maven库中得手动加入ojdbc。
19c对应的的是ojdbc10.jar
需要的jdk是
JDK8 JDK9 JDK10 (官方视频如是说)
现在稳定版本是JDK11.JDK13…EMMMMM试了试老报错。换回11了。新版本就是坑多。
cmd输入以下代码(好多实例DgroupId少了.jdbc。报错好久。坑死。)

mvn install:install-file -DgroupId=com.oracle.jdbc -DartifactId=ojdbc10 -Dversion=19.3.0.0 -Dpackaging=jar -Dfile=E:\Maven\apache-maven-3.5.4\lib\ojdbc10.jar

修改一下pom.xml,添加以下代码


     
      com.oracle.jdbc
      ojdbc10
      19.3.0.0
    

创建一个class。写入简单的连接(只是简单的测试连接。所以全写一个了。只取了五行做测试。)

package simple;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
import java.sql.DatabaseMetaData;

public class DataSourceSample {
	
	final static String DB_URL= "jdbc:oracle:thin:@localhost:1521/orcl";
	final static String DB_USER = "hr";
	final static String DB_PASSWORD = "hr";
	
	
	public static void main(String[] args) throws SQLException {
		Properties info = new Properties();     
	    info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
	    info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);          
	    info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
 
	
	    OracleDataSource ods = new OracleDataSource();
	    ods.setURL(DB_URL);    
	    ods.setConnectionProperties(info);

	    // With AutoCloseable, the connection is closed automatically.
	    try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
	      // Get the JDBC driver name and version 
	      DatabaseMetaData dbmd = connection.getMetaData();       
	      System.out.println("Driver Name: " + dbmd.getDriverName());
	      System.out.println("Driver Version: " + dbmd.getDriverVersion());
	      // Print some connection properties
	      System.out.println("Default Row Prefetch Value is: " + 
	         connection.getDefaultRowPrefetch());
	      System.out.println("Database Username is: " + connection.getUserName());
	      System.out.println();
	      // Perform a database operation 
	      printEmployees(connection);
	    }   
	  }
	 /*
	  * Displays first_name and last_name from the employees table.
	  */
	  public static void printEmployees(Connection connection) throws SQLException {
	    try (Statement statement = connection.createStatement()) {      
	      try (ResultSet resultSet = statement
	          .executeQuery("select first_name, last_name from employees fetch next 5 rows only")) {
	        System.out.println("first_name" + "  " + "last_name");
	        System.out.println("---------------------");
	        while (resultSet.next())
	          System.out.println(resultSet.getString(1) + " "
	              + resultSet.getString(2) + " ");       
	      }
	    }   
	}

}

其中 1521后写自己的数据库的主机名 db_users和password是自己的用户及密码

final static String DB_URL= "jdbc:oracle:thin:@localhost:1521/orcl";
final static String DB_USER = "hr";
final static String DB_PASSWORD = "hr";

写好后保存。运行。得到下面的

Driver Name: Oracle JDBC driver
Driver Version: 19.3.0.0.0
Default Row Prefetch Value is: 20
Database Username is:

(后面省略

你可能感兴趣的:(Oracle19c 连接java 简单demo (仅数据库连接))