package demo;
import java.sql.*;
import java.text.SimpleDateFormat;
public class JDBC_DB1 {
public static void main(String args[]) {
Connection con = null;//資料庫連接物件
Statement stmt = null;//陳述句物件,執行靜態的SQL陳述句
PreparedStatement pstmt = null;//預編譯陳述句物件,可執行動態SQL陳述句
CallableStatement cstmt = null;//陳述句物件,可執行存盤程序
ResultSet rs = null;//結構集物件
String sqlstr = "";
String url = "jdbc:postgresql://127.0.0.1:5433/demo";
String usr = "postgres";
String psd = "050037";
try {
//加載合適的jdbc驅動
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//建立資料庫連接
//con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=HISDB","sa","chunlei");
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(url, usr, psd);
//設定資料庫操作部自動提交
con.setAutoCommit(false);
stmt = con.createStatement();
// 對病人資料表進行檢索,獲取最后的病人資訊
sqlstr = " select * from Patient order by length(pID) desc, substring(pID,2,10) desc limit 1";
rs = stmt.executeQuery(sqlstr);
int pIDmax=-1;//最大病人編號中的數字
if(rs.next()) {
String pID=rs.getString("pID").trim();
System.out.println(pID + "->"
+ rs.getString("pName").trim());
pIDmax=Integer.parseInt(pID.toLowerCase().replace("p",""));
}
//采用Statement,插入病人資料
sqlstr = "insert into Patient(pID,pName) values('p" + (pIDmax + 1) + "','章彬')";
int num = stmt.executeUpdate(sqlstr);
if(num==1)System.out.println("Statement插入記錄成功!");
//采用Statement,修改病人資料
sqlstr = "update Patient set Job='student' where pName like '%章%'";
num = stmt.executeUpdate(sqlstr);
if(num>0)System.out.println("Statement修改記錄成功!");
//采用Statement,洗掉病人資料
sqlstr = "delete from Patient where pID = 'p1'";
num = stmt.executeUpdate(sqlstr);
if(num>0)System.out.println("Statement洗掉記錄成功!");
//提交以上的新增、修改、洗掉操作事務
con.commit();
//設定事務自動提交
con.setAutoCommit(true);
// 執行檢索記錄的預編譯PreparedStatement,獲取所有“學生”病人
sqlstr = "select * from Patient where Job=? ";
pstmt = con.prepareStatement(sqlstr);
pstmt.setString(1,"學生");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("pID").trim() + ":"
+ rs.getString("pName"));
}
// 采用預編譯PreparedStatement,插入病人資料
sqlstr = "insert into patient(pID,pName) values(?,?)";
pstmt = con.prepareStatement(sqlstr);
pstmt.setString(1,"p"+(pIDmax+2));
pstmt.setString(2,"王麗");
num =pstmt.executeUpdate();
if(num==1)System.out.println("PreparedStatement插入記錄成功!");
// 采用預編譯PreparedStatement,修改病人資料
sqlstr = "update patient set pName=? Where pID=?";
pstmt = con.prepareStatement(sqlstr);
pstmt.setString(1,"王林");
pstmt.setString(2,"p2");
num =pstmt.executeUpdate();
if(num>0)System.out.println("PreparedStatement修改記錄成功!");
// 采用預編譯PreparedStatement,洗掉病人資料
sqlstr = "delete from patient where pID=?";
pstmt = con.prepareStatement(sqlstr);
pstmt.setString(1,"p3");
num =pstmt.executeUpdate();
if(num>0)System.out.println("PreparedStatement洗掉記錄成功!");
// 執行檢索資料的存盤程序Query_p,存盤程序定義見存盤程序章節
cstmt = con.prepareCall("{call Query_p (?)}");
cstmt.setString(1, "李");
rs =cstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("pID").trim() + ":"
+ rs.getString("pName"));
}
// 執行添加資料的存盤程序Insert_P,存盤程序定義見存盤程序章節
cstmt = con.prepareCall("{call Insert_P(?,?,?)}");
cstmt.setString(1, "章彬");
cstmt.setString(2, "男");//性別
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date birth = new java.sql.Date(bartDateFormat.parse("1985-10-25").getTime());
cstmt.setDate(3,birth);
num =cstmt.executeUpdate();
if(num==1)System.out.println("CallableStatement插入記錄成功!");
// 執行修改資料的存盤程序Update_P,存盤程序定義見存盤程序章節
cstmt = con.prepareCall("{call Update_P(?,?)}");
cstmt.setString(1, "p6");
cstmt.setString(2, "teacher");
num =cstmt.executeUpdate();
if(num==1)System.out.println("CallableStatement修改記錄成功!");
// 執行醫生診治病人總數的存盤程序Doctor_num,定義見存盤程序章節
int result=0;
cstmt = con.prepareCall("{call Doctor_num(?, ?)}");
cstmt.setString(1, "d4");
cstmt.setInt(2, result);
cstmt.registerOutParameter(2,Types.INTEGER);
cstmt.executeUpdate();
result=cstmt.getInt(2);
System.out.println("CallableStatement d4 診治了"+result+"個病人!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
可以對它進行插入,修改操作,但是在存盤的時候就報錯了

然后在網上也找不到解決的辦法,求大佬們幫幫忙!
uj5u.com熱心網友回復:
呼叫存盤程序出問題的嗎uj5u.com熱心網友回復:
// 執行檢索資料的存盤程序Query_p報錯了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84855.html
標籤:Eclipse

