请问JDBC 连ORCACLE 11G 的代码

解决方案 »

  1.   

    /*BasicExample3.java shows how to use the Oracle JDBC extensionsto add a row to the customers table, and then retrieve that row*/// import the JDBC packagesimport java.sql.*;// import the Oracle JDBC extension packagesimport oracle.sql.*;import oracle.jdbc.*;public class BasicExample3 {public static void main (String args []) {try {// register the Oracle JDBC driversDriverManager.registerDriver(new oracle.jdbc.OracleDriver());// EDIT AS NECESSARY TO CONNECT TO YOUR DATABASE// create a Connection object, and connect to the database// as the store user using the Oracle JDBC Thin driverConnection myConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","store","store_password");// disable auto-commit modemyConnection.setAutoCommit(false);// create an oracle.sql.NUMBER objectoracle.sql.NUMBER customerId = new oracle.sql.NUMBER(6);int customerIdInt = customerId.intValue();System.out.println("customerIdInt = " + customerIdInt);// create two oracle.sql.CHAR objectsoracle.sql.CharacterSet myCharSet =CharacterSet.make(CharacterSet.US7ASCII_CHARSET);oracle.sql.CHAR firstName = new oracle.sql.CHAR("Jason", myCharSet);String firstNameString = firstName.stringValue();System.out.println("firstNameString = " + firstNameString);oracle.sql.CHAR lastName = new oracle.sql.CHAR("Price", myCharSet);System.out.println("lastName = " + lastName);// create an oracle.sql.DATE objectoracle.sql.DATE dob = new oracle.sql.DATE("1969-02-22 13:54:12");String dobString = dob.stringValue();System.out.println("dobString = " + dobString);// create an OraclePreparedStatement objectOraclePreparedStatement myPrepStatement =(OraclePreparedStatement) myConnection.prepareStatement("INSERT INTO customers " +"(customer_id, first_name, last_name, dob, phone) VALUES (" +"?, ?, ?, ?, ?" +")");// bind the objects to the OraclePreparedStatement using the// appropriate set methodsmyPrepStatement.setNUMBER(1, customerId);myPrepStatement.setCHAR(2, firstName);myPrepStatement.setCHAR(3, lastName);myPrepStatement.setDATE(4, dob);// set the phone column to NULLmyPrepStatement.setNull(5, OracleTypes.CHAR);// run the PreparedStatementmyPrepStatement.execute();System.out.println("Added row to customers table");// retrieve the ROWID, customer_id, first_name, last_name, dob, and// phone columns for this new row using an OracleResultSet// objectStatement myStatement = myConnection.createStatement();OracleResultSet customerResultSet =(OracleResultSet) myStatement.executeQuery("SELECT ROWID, customer_id, first_name, last_name, dob, phone " +"FROM customers " +"WHERE customer_id = 6");System.out.println("Retrieved row from customers table");// declare an oracle.sql.ROWID object to store the ROWID, and// an oracle.sql.CHAR object to store the phone columnoracle.sql.ROWID rowid;oracle.sql.CHAR phone = new oracle.sql.CHAR("", myCharSet);// display the column values for row using the// get methods to read the valueswhile (customerResultSet.next()) {rowid = customerResultSet.getROWID("ROWID");customerId = customerResultSet.getNUMBER("customer_id");firstName = customerResultSet.getCHAR("first_name");lastName = customerResultSet.getCHAR("last_name");dob = customerResultSet.getDATE("dob");phone = customerResultSet.getCHAR("phone");System.out.println("rowid = " + rowid.stringValue());System.out.println("customerId = " + customerId.stringValue());System.out.println("firstName = " + firstName);System.out.println("lastName = " + lastName);System.out.println("dob = " + dob.stringValue());System.out.println("phone = " + phone);} // end of while loop// close the OracleResultSet object using the close() methodcustomerResultSet.close();// roll back the changes made to the databasemyConnection.rollback();// close the other JDBC objectsmyPrepStatement.close();myConnection.close();} catch (SQLException e) {System.out.println("Error code = " + e.getErrorCode());System.out.println("Error message = " + e.getMessage());System.out.println("SQL state = " + e.getSQLState());e.printStackTrace();}} // end of main()}
      

  2.   

    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.150:1521:testdb","system","oracle");   这两句关键的要写对
      

  3.   

    可以使用OracleDataSource嘛:下面一个获得Connection对象的简单方法:
    public Connection getConnection()
    {
        Connection conn = null;
        try
        {
            OracleDataSource ods = new OracleDataSource();
            ods.setUser("user"); // 用户名
            ods.setPassword("password"); // 密码
            ods.setDriverType("thin"); // 连接方式
            ods.setServerName("192.168.100.28"); // 主机IP
            ods.setPortNumber(1521); // 端口号
            ods.setDatabaseName("orcl"); // 数据库名称
            conn = ods.getConnection();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return conn;
    }
    这样多好多方便......