?? test.java
字號(hào):
package untitled3;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.sql.*;
public class test {
Connection conn = null;
java.sql.Statement stmt = null;
//java.sql.CallableStatement stmt = null;
ResultSet rs = null;
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://192.168.0.69:5432/yefei_db";
String user = "postgres";
String passwd = "postgres";
//過(guò)程函數(shù)的sql語(yǔ)句
public test() {
}
private void insert_db(){
//You can change the values to test the trigger
String insertsql = "insert into emp(empname,salary) values('eee',1)";
String Id;
//It's the table structure
/*CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
);*/
try {
Class.forName(driver);
//連接數(shù)據(jù)庫(kù)
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("連接數(shù)據(jù)庫(kù)成功");
}
//準(zhǔn)備可調(diào)用語(yǔ)句對(duì)象
stmt = conn.createStatement();
stmt.execute(insertsql);
System.out.println("insertsql被調(diào)用");
}
catch (Exception e) {
//出錯(cuò)處理
e.printStackTrace();
}
finally {
try {
if (stmt != null) {
//關(guān)閉狀態(tài)
stmt.close();
}
if (conn != null) {
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private void delete_db(){
//You can change the values to test the trigger
String emp11="eee";
String deletesql = "delete from emp where salary=" + 1;
String Id;
//It's the table structure
/*CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
);*/
try {
Class.forName(driver);
//連接數(shù)據(jù)庫(kù)
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("連接數(shù)據(jù)庫(kù)成功");
}
//準(zhǔn)備可調(diào)用語(yǔ)句對(duì)象
stmt = conn.createStatement();
stmt.executeUpdate(deletesql);
System.out.println("insertsql被調(diào)用");
}
catch (Exception e) {
//出錯(cuò)處理
e.printStackTrace();
}
finally {
try {
if (stmt != null) {
//關(guān)閉狀態(tài)
stmt.close();
}
if (conn != null) {
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private void test_fuction(){
String select = "select pgsql_test()";
String Id;
try {
Class.forName(driver);
//連接數(shù)據(jù)庫(kù)
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("連接數(shù)據(jù)庫(kù)成功");
}
//準(zhǔn)備可調(diào)用語(yǔ)句對(duì)象
stmt = conn.createStatement();
rs = stmt.executeQuery(select);
System.out.println("過(guò)程函數(shù)被調(diào)用");
//獲取來(lái)自結(jié)果集中的數(shù)據(jù)
while (rs.next()) {
//打印輸出參數(shù)的值
Id = rs.getString(1);
//獲取輸出參數(shù)的值
System.out.println("返回值為 " + Id);
}
}
catch (Exception e) {
//出錯(cuò)處理
e.printStackTrace();
}
finally {
try {
// Always close properly
if (rs != null) {
//關(guān)閉結(jié)果集
rs.close();
}
if (stmt != null) {
//關(guān)閉狀態(tài)
stmt.close();
}
if (conn != null) {
//關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test test1 = new test();
//測(cè)試存儲(chǔ)過(guò)程
test1.test_fuction();
//測(cè)試insert觸發(fā)器
test1.insert_db();
//測(cè)試delete_db觸發(fā)器
//test1.delete_db();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -