• Java连接Oracle


    Process myProcess = Runtime.getRuntime().exec("ipconfig");
    InputStreamReader ir = new InputStreamReader(myProcess.getInputStream());
    LineNumberReader input = new LineNumberReader (ir);
    String line;
    while ((line = input.readLine ()) != null)
    System.out.println(line);

    2.jdbc的thin方式 
      此种方法不需要安装Oracle的客户端,也不需要配置odbc,故此种方法用得比较普遍。 
      此方法在使用时需要将oracle的jar包加到classpath变量中,此包可以在oralce客户端程序的$ORACLE_HOME/jdbc/lib/classes12.jar找到.

    import java.sql.*;
    public class jdbcthin {
    //dbUrl数据库连接串信息,其中“1521”为端口,“ora9”为sid
    String dbUrl = "jdbc:oracle:thin:@10.10.20.15:1521:ora9";
    //theUser为数据库用户名
    String theUser = "sman";
    //thePw为数据库密码
    String thePw = "sman";
    //几个数据库变量
    Connection c = null;
    Statement conn;
    ResultSet rs = null;
    //初始化连接
    public jdbcthin() {
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    //与url指定的数据源建立连接
    c = DriverManager.getConnection(dbUrl, theUser, thePw);
    //采用Statement进行查询
    conn = c.createStatement();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    //执行查询
    public ResultSet executeQuery(String sql) {
    rs = null;
    try {
    rs = conn.executeQuery(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return rs;
    }
    public void close() {
    try {
    conn.close();
    c.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    ResultSet newrs;
    jdbcthin newjdbc = new jdbcthin();
    newrs = newjdbc.executeQuery("select * from eventtype");
    try {
    while (newrs.next()) {
    System.out.print(newrs.getString("event_type"));
    System.out.println(":"+newrs.getString("content"));
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    newjdbc.close();
    }
    }

  • 相关阅读:
    用户体验评价
    第十三周总结
    第十二周总结
    单词统计
    第十一周总结
    冲刺(十一)
    用户模板和用户场景
    冲刺(十)
    冲刺(九)
    IOS 学习记录
  • 原文地址:https://www.cnblogs.com/skyay/p/3706423.html
Copyright © 2020-2023  润新知