📄 example2.java
字号:
/*
* WebLogic Server Unleashed
*
*/
package com.wlsunleashed.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* This class demonstrates the use of the Connection object to insert
* and delete rows from a database table.
*
* @version 1.0
*/
public class Example2 {
/**
* 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) {
Example2 obj = new Example2();
obj.insertRows();
}
/**
* 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
*
* @param host : The host where the server is running
* @param port : The port where the server is listening to
* @param dbName : the database name
* @param userid : The user id to use while connecting
* @param password : The password of the specified user
*
* @return An object of type java.sql.Connection
*/
private Connection getConnection(
String host,
int port,
String dbName,
String userid,
String password) {
Connection conn = null;
try {
Class.forName("com.pointbase.jdbc.jdbcUniversalDriver");
String url =
"jdbc:pointbase:server://" + host + ":" + port + "/" + dbName;
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;
}
/**
* This method delete all items from the ITEM table
*
* @param aConnection : The connection to be used
*/
private void deleteAllItems(Connection aConnection) {
String anSQLStmt = " DELETE FROM XYZCONF.ITEM ";
executeUpdate(aConnection, anSQLStmt);
}
/**
* This is a generic executeUpdate method which executes the SQL statement
* passed in as a parameter. The connection is also passed in as a
* parameter.
*
* @param aConnection : The connection to be used
* @param anSQLStmt : The statement to be executed
*
* @return The number of rows affected. If update
* fails, the method returns a -1
*/
private int executeUpdate(Connection aConnection, String anSQLStmt) {
int numRowsAffected = -1;
try {
System.out.println("Executing statement : " + anSQLStmt);
Statement stmt = aConnection.createStatement();
numRowsAffected = stmt.executeUpdate(anSQLStmt);
System.out.println("Number of rows affected: " + numRowsAffected);
stmt.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return numRowsAffected;
}
/**
* This method inserts one item into the ITEM table. the values of the row
* are obtained as parameters to this method
*
* @param aConnection : The connection to use
* @param id : The id of the item
* @param desc : The description of the item
* @param stock : the stock of the item
* @param unitPrice : unit price of the item
*/
private void insertOneItem(
Connection aConnection,
int id,
String desc,
double stock,
double unitPrice) {
String anSQLStmt =
" INSERT INTO XYZCONF.ITEM VALUES ( "
+ id
+ ",'"
+ desc
+ "',"
+ stock
+ ","
+ unitPrice
+ " )";
if (executeUpdate(aConnection, anSQLStmt) != 1) {
System.out.println("Insert failed");
} else {
System.out.println("Insert succeeded");
};
}
/**
* inserts five rows into the ITEM table
*/
private void insertRows() {
System.out.println("Trying to obtain a connection ... ");
Connection conn =
getConnection(
"localhost",
9092,
"demo",
"PBSYSADMIN",
"PBSYSADMIN");
if (conn == null) {
return;
}
System.out.println("Connection obtained !");
System.out.println("Emptying item table first ");
deleteAllItems(conn);
insertOneItem(conn, 1, "Candy", 50.0, 2.0);
insertOneItem(conn, 2, "Crunchies", 24.0, 1.4);
insertOneItem(conn, 3, "Snickers", 40.0, 2.3);
insertOneItem(conn, 4, "Polo", 55.0, 2.0);
insertOneItem(conn, 5, "Wafers", 40.0, 2.55);
try {
conn.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -