作为Oracle DBA,有时候也需要熟悉点开发语言。C很强大,Oracle数据库就是用C写的,不是C++,有超过2500万行代码。但要用好C,相对java来说,难度还是很大的。Oracle数据库在世界上是关系型数据库的Number one,在高可用方面也有很多特性。比如在主备环境中做切换,还可以保证业务的连续性。如果要测试这个特性,你就要写代码,虽然官方也有测试代码。本文适合Java入门级选手,只当抛砖引玉。
Java教程下载链接
可以直接从下面的链接中下载,文件大小143M。只是全英的。
https://www.oracle.com/java/technologies/javase-java-tutorial-downloads.html
https://docs.oracle.com/javase/tutorial/index.htmlJVM,JRE,JDK之间的关系一目了然
从下图我们也能很容易的看出JVM,JRE,JDK之间的关系。可直接打开链接https://docs.oracle.com/javase/8/docs/index.html,图片中的每一部分都有链接,可直接查看
Java SE,Java EE,Java ME,Java FX的区别
我们可能听过这些名词,有时候也会迷惑,下面我们看一下他们之间的区别。
Java SE
当大多数人想到Java编程语言时,我们想到的是Java SE API。Java SE的API提供了Java编程语言的核心功能。它定义了从Java编程语言的基本类型和对象到用于网络、安全、数据库访问、图形用户界面(GUI)开发和XML解析的高级类的所有内容。除了核心API之外,Java SE平台还包含一个虚拟机、开发工具、部署技术,以及Java技术应用程序中通常使用的其他类库和工具包。
Java EE
Java EE平台构建在Java SE平台之上。Java EE平台为开发和运行大型、多层次、可伸缩、可靠和安全的网络应用程序提供了API和运行时环境。Java ME
Java ME平台提供了一个API和一个占用空间很小的虚拟机,用于在移动电话等小型设备上运行Java编程语言应用程序。该API是Java SE API的一个子集,还有用于小型设备应用程序开发的特殊类库。Java ME应用程序通常是Java EE平台服务的客户端。JavaFX
JavaFX是一个使用轻量级用户接口API创建Internet应用程序的平台。JavaFX应用程序使用硬件加速的图形和媒体引擎来利用高性能客户端和现代外观,以及用于连接网络数据源的高级API。JavaFX应用程序可以是Java EE平台服务的客户端
J2SE和J2EE的关系
EJB - Enterprise JavaBeans for distributed component systems
JNDI - Java Naming and Directory Interface is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name
JDBC - Java DataBase connector is a set of API T that defines how a client may access and execute query on database
JTA - Java Transaction API is a set of API for distributed transaction.
JAXP - API for XML files.
JMS - Java Message Service is a Java based Message Oriented Middleware.
一步步使用JDBC,可按照教程使用
https://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
https://docs.oracle.com/javase/tutorial/jdbc/basics/examples/zipfiles/JDBCTutorial.zip
注意:本教程中的示例使用DriverManager类而不是DataSource类,因为相比来说,DriverManager更容易使用。在生产环境中建议使用DataSource。如果使用Oracle数据库,建议使用Oracle的通用连接池UCP.jar。
建立到数据库的链接
https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html
DriverManager: This fully implemented class connects an application to a data source, which is specified by a database URL. When this class first attempts to establish a connection, it automatically loads any JDBC 4.0 drivers found within the class path. Note that your application must manually load any JDBC drivers prior to version 4.0.
DataSource: This interface is preferred over DriverManager because it allows details about the underlying data source to be transparent to your application. A DataSource object's properties are set so that it represents a particular data source. See Connecting with DataSource Objects for more information. For more information about developing applications with the DataSource class, see the latest The Java EE Tutorial.
JDBC使用一个类java.sql.DriverManager和两个接口java.sql.Driver,java.sql.Connection用于连接数据库。下面看下简单的描述
java.sql.Driver:除非您正在编写自己的自定义JDBC,否则永远都不应该从应用程序中处理此类。它只是通过响应DriverManager连接请求并提供有关实现的信息,为数据库连接提供了一个启动点。
java.sql.DriverManager:DriverManager是一个类,而不是接口。它的主要职责是维护一个驱动程序实现列表,并提供一个与请求的URL匹配的应用程序驱动。DriverManager提供了registerDriver()和deregisterDriver()方法,它们允许驱动程序实现向驱动程序管理器注册自身,或者将自己从列表中删除。您可以通过getDrivers()方法获得已注册的驱动程序。
java.sql.Connection:Connection表示单个逻辑数据库连接。换句话说,使用Connection向数据库发送一系列SQL语句,并管理这些语句的提交或回滚。
https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html
https://docs.oracle.com/javase/8/docs/api/javax/sql/DataSource.html
https://docs.oracle.com/javase/8/docs/api/java/sql/package-tree.html
https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html
https://docs.oracle.com/javase/8/docs/api/java/sql/Driver.html
https://docs.oracle.com/javadb/10.6.2.1/ref/crefjdbc12657.html
https://docs.oracle.com/cd/E13222_01/wls/docs45/classdocs/Package-java.sql.html
https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
有三种不同的statements
Statement: 用来执行简单的SQL,并且不需要输入参数
PreparedStatement: (Extends Statement.) 这个是Statement的延伸,用于预编译SQL语句,该语句可能带有输入参数。就像我们在Oracle数据库中建的带参数的存储过程一样。语句被预编译并且存储在PreparedStatement 对象中,然后可以使用此对象高效地多次执行该语句。如果要多次执行,建议使用。可通过setString,setInt等做输入参数。请参考https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
PreparedStatement updateSales = null;
String updateString =
"update " + dbName + ".COFFEES " +
"set SALES = ? where COF_NAME = ?";
updateSales = con.prepareStatement(updateString);
updateSales.setInt(1, e.getValue().intValue());
updateSales.setString(2, e.getKey());
CallableStatement: (Extends PreparedStatement.) 用来执行可能带有输入和输出参数的存储过程
Java脚本样例
将下面脚本保存为BulkInsertTest.java,编译时加上oracle的JDBC驱动jar包(ojdbc8.jar)就可编译通用。然后就可以用于测试批量提交了。import java.sql.*; import java.math.*; import oracle.jdbc.pool.*; import oracle.jdbc.*; import java.lang.*; import java.io.Console; import java.util.Random; import java.util.Scanner; import java.security.SecureRandom; public class BulkInsertTest { private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase(); private static final String NUMBER = "0123456789"; private static final String DATA_FOR_RANDOM_STRING = CHAR_LOWER + CHAR_UPPER + NUMBER; private static SecureRandom random = new SecureRandom(); public static String generateRandomChars(int length) { if (length < 1) throw new IllegalArgumentException(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { int rndCharAt = random.nextInt(DATA_FOR_RANDOM_STRING.length()); char rndChar = DATA_FOR_RANDOM_STRING.charAt(rndCharAt); // debug //System.out.format("%d\t:\t%c%n", rndCharAt, rndChar); sb.append(rndChar); } return sb.toString(); } public static void main(String args[]) { long start=0; long stop=0; try{ DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection c = DriverManager.getConnection ("jdbc:oracle:thin:@192.168.10.21:1521:ppdb01","system","Oracle"); PreparedStatement stmt = c.prepareStatement("insert into emp(employee_id,first_name,last_name,salary) values (?,?,?,?)"); start=System.currentTimeMillis(); int batchSize=5000; int count=0; Random r = new Random(); for (int i=0 ; i < 500000 ; i++) { stmt.setInt(1,i); stmt.setString(2,generateRandomChars(8)); stmt.setString(3,generateRandomChars(8)); stmt.setInt(4,r.nextInt(10000+1)+5000); stmt.addBatch(); if (++count % batchSize == 0) { stmt.executeBatch(); } } stop=System.currentTimeMillis(); System.out.println("time = "+(stop-start)); stmt.close(); c.close(); } catch(Exception e){ System.err.println(e); e.printStackTrace(); } } }java开发者的两天教程 https://docs.oracle.com/en/database/oracle/oracle-database/19/tdpjd/overview-hr-web-application.html
https://docs.oracle.com/en/database/oracle/oracle-database/19/development.html
J2EE和.NET的区别
https://sites.google.com/site/sureshdevang/j2ee-vs--net
The JNDI Tutorial
https://docs.oracle.com/javase/jndi/tutorial/index.html
JDBC和Oracle方面的编程
下面的链接和代码是对应的,有兴趣的可以看看
https://datubaze.files.wordpress.com/2015/11/kuasi_mensah_oracle_database_prog_using_java_2005.pdf
https://download.oracle.com/technology/tech/java/jsp/pdf/Code_depot.zip
总结:连接Oracle数据库的方式有很多,比如java.sql.DriverManager,javax.sql.DataSource,Apache Commons DBCP,C3P0,Vibur,Hikari,UCP等。建议使用Oracle的通用连接池(UCP),被封装到一个文件ucp.jar。无论是性能还是高可用方面都很强悍!
Reference
https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/index.html
https://www.oracle.com/java/technologies/java-ee-glance.html
https://blogs.oracle.com/java/java-ee-8-overviewhttps://docs.oracle.com/javadb/index_jdk8.html
https://javaee.github.io/javamail/
https://docs.oracle.com/javase/tutorial/index.html
https://blogs.oracle.com/thejavatutorials/
https://docs.oracle.com/cd/E19263-01/817-5764/architecture.html
https://javaee.github.io/
https://www.oracle.com/technetwork/cn/java/javase/tech/index.html
https://www.oracle.com/technetwork/cn/java/javaee/tech/index.html
https://www.oracle.com/cn/java/technologies/java-ee-glance.html
https://download.oracle.com/otn_hosted_doc/jdeveloper/1012/developing_mvc_applications/adf_aoverviewadfandj2ee.html
http://www.cs.mun.ca/java-api-1.5/
https://javaee.github.io/javaee-spec/download/JavaEE8_Platform_Spec_FinalRelease.pdf
https://docs.oracle.com/javaee/7/index.html
https://netbeans.org/kb/docs/javaee/ecommerce/intro.html
https://netbeans.apache.org/download/index.html
https://www.oracle.com/technetwork/java/javaseproducts/overview/index.html
https://docs.oracle.com/cd/E18727_01/doc.121/e12064/T291171T509750.htm
https://docs.oracle.com/cd/E17802_01/j2ee/j2ee/1.4/docs/tutorial-update2/doc/Overview7.html
http://jdk.java.net/java-se-ri/14
https://sites.google.com/site/ingpaololatella/java/java-ee-java-se-and-java-me
https://www.oracle.com/technical-resources/articles/java/deployment-tools.html
http://www.idevelopment.info/html/PROGRAMMING_Java_Programming.shtml
https://datubaze.files.wordpress.com/2015/11/kuasi_mensah_oracle_database_prog_using_java_2005.pdf