📄 test.java
字号:
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";
//过程函数的sql语句
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);
//连接数据库
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("连接数据库成功");
}
//准备可调用语句对象
stmt = conn.createStatement();
stmt.execute(insertsql);
System.out.println("insertsql被调用");
}
catch (Exception e) {
//出错处理
e.printStackTrace();
}
finally {
try {
if (stmt != null) {
//关闭状态
stmt.close();
}
if (conn != null) {
//关闭数据库连接
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);
//连接数据库
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("连接数据库成功");
}
//准备可调用语句对象
stmt = conn.createStatement();
stmt.executeUpdate(deletesql);
System.out.println("insertsql被调用");
}
catch (Exception e) {
//出错处理
e.printStackTrace();
}
finally {
try {
if (stmt != null) {
//关闭状态
stmt.close();
}
if (conn != null) {
//关闭数据库连接
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private void test_fuction(){
String select = "select pgsql_test()";
String Id;
try {
Class.forName(driver);
//连接数据库
conn = DriverManager.getConnection(url, user, passwd);
if (conn != null) {
System.out.println("连接数据库成功");
}
//准备可调用语句对象
stmt = conn.createStatement();
rs = stmt.executeQuery(select);
System.out.println("过程函数被调用");
//获取来自结果集中的数据
while (rs.next()) {
//打印输出参数的值
Id = rs.getString(1);
//获取输出参数的值
System.out.println("返回值为 " + Id);
}
}
catch (Exception e) {
//出错处理
e.printStackTrace();
}
finally {
try {
// Always close properly
if (rs != null) {
//关闭结果集
rs.close();
}
if (stmt != null) {
//关闭状态
stmt.close();
}
if (conn != null) {
//关闭数据库连接
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test test1 = new test();
//测试存储过程
test1.test_fuction();
//测试insert触发器
test1.insert_db();
//测试delete_db触发器
//test1.delete_db();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -