• 转 Java连接Oracle数据库的简单示例


    https://www.cnblogs.com/joyny/p/11176643.html

    https://community.oracle.com/thread/4096458


    import java.io.PrintStream;
    import java.sql.*;
    import java.util.Properties;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;

    public class ZJdbcPing {
    public static void main(String[] args) {
    PrintStream outs = System.out;
    // title.
    outs.println("zjdbcping <driverClassName> <url> <username> <password>");
    // oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@192.168.3.33:1521:zjkf" crjapp crjapp
    outs.println();
    // args.
    String driverClassName="";
    String url="";
    String username="";
    String password="";
    if (args.length>0) driverClassName=args[0];
    if (args.length>1) url=args[1];
    if (args.length>2) username=args[2];
    if (args.length>3) password=args[3];
    // connect.
    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet result = null;
    try {
    Class.forName(driverClassName);
    conn = DriverManager.getConnection(url, username, password);
    StringBuilder sql = new StringBuilder();
    sql.append("select instance_name from v$instance");
    statement = conn.prepareStatement(sql.toString());
    result = statement.executeQuery();
    int i = 0;
    while(result.next()){
    // System.out.println(i + result.toString());
    System.out.println(String.format(">>>>> Server : [%s]", result.getString(1)));
    i++;
    }
    }
    catch(Throwable ex) {
    ex.printStackTrace(outs);
    }
    if (null==conn) return;
    // info.
    try {
    outs.println(String.format("getCatalog: %s", conn.getCatalog()));
    outs.println("getClientInfo:");
    Properties properties = conn.getClientInfo();
    properties.list(outs);
    //outs.println("getTypeMap:");
    //Map<String,Class<?>> typeMap = conn.getTypeMap();
    //for(String key: typeMap.keySet()) {
    // Class<?> c = typeMap.get(key);
    // outs.println(String.format(" %s: %s", key, c.toString()));
    //}
    outs.println("getMetaData:");
    DatabaseMetaData databaseMetaData = conn.getMetaData();
    printDatabaseMetaData(outs, databaseMetaData, " ");
    }
    catch(Throwable ex) {
    ex.printStackTrace(outs);
    }
    finally {
    try {
    conn.close();
    }
    catch(Throwable ex) {
    ex.printStackTrace(outs);
    }
    }
    return;
    }

    private static void printDatabaseMetaData(PrintStream outs,
    DatabaseMetaData dbmd, String prefix) throws Exception {
    if (null==outs) return;
    if (null==dbmd) return;
    outs.println(prefix+String.format("getDatabaseMajorVersion: %d", dbmd.getDatabaseMajorVersion()));
    outs.println(prefix+String.format("getDatabaseMinorVersion: %d", dbmd.getDatabaseMinorVersion()));
    outs.println(prefix+String.format("getDatabaseProductName: %s", dbmd.getDatabaseProductName()));
    outs.println(prefix+String.format("getDatabaseProductVersion: %s", dbmd.getDatabaseProductVersion()));
    outs.println(prefix+String.format("getDefaultTransactionIsolation: %d", dbmd.getDefaultTransactionIsolation()));
    outs.println(prefix+String.format("getDriverMajorVersion: %d", dbmd.getDriverMajorVersion()));
    outs.println(prefix+String.format("getDriverMinorVersion: %d", dbmd.getDriverMinorVersion()));
    outs.println(prefix+String.format("getDriverName: %s", dbmd.getDriverName()));
    outs.println(prefix+String.format("getDriverVersion: %s", dbmd.getDriverVersion()));
    outs.println(prefix+String.format("getJDBCMajorVersion: %d", dbmd.getJDBCMajorVersion()));
    outs.println(prefix+String.format("getJDBCMinorVersion: %d", dbmd.getJDBCMinorVersion()));
    outs.println(prefix+String.format("getURL: %s", dbmd.getURL()));
    outs.println(prefix+String.format("getUserName: %s", dbmd.getUserName()));
    ResultSet rs = dbmd.getClientInfoProperties();
    outs.println(prefix+"getClientInfoProperties:");
    printResultSet(outs, rs, prefix+" ");
    rs.close();
    }

    private static void printResultSet(PrintStream outs, ResultSet rs,
    String prefix) throws Exception {
    if (null==outs) return;
    if (null==rs) return;
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    outs.println(prefix+String.format("#getColumnCount: %d", cols));
    // col.
    if (true) {
    outs.print(prefix);
    for(int i=1; i<=cols; ++i) {
    if (i>1) outs.print(' ');
    String str = rsmd.getColumnName(i);
    outs.print(str);
    }
    outs.println();
    }
    // rows.
    int rows = 0;
    while(rs.next()) {
    ++rows;
    outs.print(prefix);
    for(int i=1; i<=cols; ++i) {
    if (i>1) outs.print(' ');
    Object o = null;
    String str = null;
    try {
    o = rs.getObject(i);
    if (null!=o)
    str = o.toString();
    }
    catch(Exception ex) {
    str = String.format("(%s)", ex.getMessage());
    }
    outs.print(str);
    }
    outs.println();
    }
    outs.println(prefix+String.format("#RowCount: %d", rows));
    }

    }
    复制代码
    java打jar包
    方法步骤

    (1)用命令行进入到该目录下,编译这个文件

       javac ZJdbcPing.java

    (2)将编译后的ZJdbcPing.class文件打成jar包

       jar -cvf ZJdbcPing.jar ZJdbcPing.class

      c表示要创建一个新的jar包,v表示创建的过程中在控制台输出创建过程的一些信息,f表示给生成的jar包命名

    (3) java -jar ZJdbcPing.jar 这时会报如下错误 ZJdbcPing.jar中没有主清单属性

      添加Main-Class属性

      ftp到本地,用压缩软件打开ZJdbcPing.jar,会发现里面多了一个META-INF文件夹,里面有一个MENIFEST.MF的文件,用记事本打开

    1 Manifest-Version: 1.0
    2 Created-By: 1.8.0_121 (Oracle Corporation)
    3
      在第三行的位置写入 Main-Class: ZJdbcPing(注意冒号后面有一个空格,整个文件最后有一行空行),保存

    具体运行时,还注意得用java命令来运行,并指定加载驱动 jar. 例如(/usr/local/zabbix/orabbix/lib/ojdbc6.jar")——


    ##java -Xbootclasspath/a:"ojdbc6-11.2.0.3.jar" -jar ZJdbcPing.jar oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@10.10.10.245:1521:orcl" yjkj 123456
    pause

    java -Xbootclasspath/a:"/usr/local/zabbix/orabbix/lib/ojdbc6.jar" -jar ZJdbcPing.jar oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@10.10.10.245:1529:ora11g" peng peng

    output sql value:

    >>>>> Server : [2]

    location:

    [root@pzabbix02 dba]# pwd
    /backup/dba

  • 相关阅读:
    损失函数及其梯度
    激活函数及其梯度
    梯度下降简介
    误差计算
    输出方式
    全连接层
    测试(张量)- 实战
    数据加载
    高阶操作
    java网络基础知识的简述
  • 原文地址:https://www.cnblogs.com/feiyun8616/p/11511704.html
Copyright © 2020-2023  润新知