157.Oracle数据库SQL开发之 JAVA——示例程序2

来源:互联网 发布:流量监控软件哪个好 编辑:程序博客网 时间:2024/06/05 17:42

157.Oracle数据库SQL开发之 JAVA——示例程序2

欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/50185999

1.  代码如下:

import java.sql.*;

 

class Product {

 intproductId;

 intproductTypeId;

 String name;

 String description;

 doubleprice;

}

 

publicclassBasicExample2 {

 publicstaticvoidmain (Stringargs[]) {

   try{

     // register the Oracle JDBC drivers

     DriverManager.registerDriver(

        new oracle.jdbc.OracleDriver()

     );

 

     // EDIT AS NECESSARY TO CONNECT TO YOURDATABASE

     // create a Connection object, and connectto the database

     // as the store user using the Oracle JDBCThin driver

     Connection myConnection = DriverManager.getConnection(

        "jdbc:oracle:thin:@192.168.1.201:1521/pdb1",

        "store",

        "store_password"

     );

 

     // disable auto-commit mode

     myConnection.setAutoCommit(false);

 

     Product [] productArray = newProduct[5];

     for(intcounter= 0;counter<productArray.length;counter ++) {

        productArray[counter] = new Product();

        productArray[counter].productId =counter+ 13;

        productArray[counter].productTypeId = 1;

        productArray[counter].name ="Test product";

        productArray[counter].description ="Test product";

        productArray[counter].price = 19.95;

     } // end of for loop

     PreparedStatement myPrepStatement = myConnection.prepareStatement(

            "INSERT INTO products " +

            "(product_id, product_type_id, name, description, price) VALUES(" +

            "?, ?, ?, ?, ?" +

            ")"

          );

 

          // initializethe values for the new rows using the

          // appropriateset methods

          for (intcounter = 0;counter<productArray.length;counter ++) {

            myPrepStatement.setInt(1,productArray[counter].productId);

            myPrepStatement.setInt(2,productArray[counter].productTypeId);

            myPrepStatement.setString(3,productArray[counter].name);

            myPrepStatement.setString(4,productArray[counter].description);

            myPrepStatement.setDouble(5,productArray[counter].price);

            myPrepStatement.execute();

          } // end of for loop

 

          // close thePreparedStatement object

          myPrepStatement.close();

 

          // retrieve theproduct_id, product_type_id, name, description, and

          // pricecolumns for these new rows using a ResultSet object

          Statement myStatement = myConnection.createStatement();

          ResultSet productResultSet = myStatement.executeQuery(

            "SELECT product_id, product_type_id, "+

            "  name, description, price" +

            "FROM products " +

            "WHERE product_id > 12"

          );

 

          // display thecolumn values

          while (productResultSet.next()) {

            System.out.println("product_id = " +

              productResultSet.getInt("product_id"));

            System.out.println("product_type_id = " +

              productResultSet.getInt("product_type_id"));

            System.out.println("name = " +

              productResultSet.getString("name"));

            System.out.println("description = " +

              productResultSet.getString("description"));

            System.out.println("price = " +

              productResultSet.getDouble("price"));

          } // end of while loop

 

          // close theResultSet object using the close() method

          productResultSet.close();

 

          //rollbackthe changes made to the database

          myConnection.rollback();

 

          // close theother JDBC objects

          myStatement.close();

          myConnection.close();

 

        } catch (SQLExceptione) {

          System.out.println("Error code = " +e.getErrorCode());

          System.out.println("Error message = " +e.getMessage());

          System.out.println("SQL state = " +e.getSQLState());

          e.printStackTrace();

        }

      } // end ofmain()

    }

2.  输出如下:

product_id = 13

product_type_id = 1

name = Test product

description = Test product

price = 19.95

product_id = 14

product_type_id = 1

name = Test product

description = Test product

price = 19.95

product_id = 15

product_type_id = 1

name = Test product

description = Test product

price = 19.95

product_id = 16

product_type_id = 1

name = Test product

description = Test product

price = 19.95

product_id = 17

product_type_id = 1

name = Test product

description = Test product

price = 19.95

 

0 0
原创粉丝点击