📄 example6.java
字号:
/*
* WebLogic Server Unleashed
*
*/
package com.wlsunleashed.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* This class demonstrates an SQL Update using an updateable
* ResultSet
*
* @version 1.0
*/
public class Example6 {
/** The host to connect to */
private static final String HOST = "localhost";
/** The port number **/
private static final int PORT = 9092;
/** The database name **/
private static final String DBNAME = "demo";
/** User id in the database */
private static final String USERID = "PBSYSADMIN";
/** Password for the user */
private static final String PASSWORD = "PBSYSADMIN";
/**
* The main method - the point of entry into this class
*
* @param args : all run-time args are passed in this String array
*/
public static void main(String[] args) {
Example6 obj = new Example6();
obj.incrementStock();
}
private String getDBConnectionURL() {
return
"jdbc:pointbase:server://" + HOST + ":" + PORT + "/"
+ DBNAME;
}
/**
* Returns a connection to a pointbase database, whose server is running at
* the given host and port, and the database being called dbName. The
* credentials supplied while connecting is also accepted as paramter
*
* @return an object of type java.sql.Connection
*/
private Connection getConnection( ) {
Connection conn = null;
try {
Class.forName("com.pointbase.jdbc.jdbcUniversalDriver");
String url = getDBConnectionURL();
System.out.println("Using url string " + url);
conn = DriverManager.getConnection(url, USERID, PASSWORD);
System.out.println("Obtained Connection");
} catch (Throwable t) {
t.printStackTrace();
conn = null;
}
return conn;
}
/**
* increments the stock of the items in the ITEM table based on the
* stockArray passed as a parameter. The stock array is assumed to have as
* many entries as there are items in the table
*
* @param stockArray : a list of increments to the stock.
*/
private void incrementStock( ) {
System.out.println("Trying to obtain a connection ... ");
Connection conn = getConnection();
if (conn == null) {
return;
}
System.out.println("Connection obtained !");
String anSQLStmt =
" SELECT ITEM_ID, " +
" STOCK_QTY FROM XYZCONF.ITEM";
try {
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(anSQLStmt);
rs.beforeFirst();
System.out.println("Before Updating Stock for first item");
while (rs.next()) {
System.out.println("Item Code = " +
rs.getInt(1) + " stock = " +
rs.getDouble(2));
}
if (rs.first()) {
rs.updateDouble(2, rs.getDouble(2) + 10.0);
rs.updateRow();
}
rs.close();
rs = stmt.executeQuery(anSQLStmt);
//rs.beforeFirst();
System.out.println("AFTER Updating Stock for first item");
while (rs.next()) {
System.out.println("Item Code = " +
rs.getInt(1) + " stock = " +
rs.getDouble(2));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException t) {
t.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -