js|mysql
前提:
將MySQL資料庫的驅動放在工作目錄的web-inf\lib目錄下(這樣才能在JSP中連結上)
用JavaBean串連,將編譯好得.class檔案放在classes檔案下,若檔案包含package指令,則要放到
指定的目錄下。
此時,資料查詢沒問題,但是update,delete和insert都無效。(在SQL Server 中可行)
問題解決,察看JDK說明,找到Statement的方法段ResultSet executeQuery(String), int executeUpdate(String)
修改JavaBean,添加executeUpdate方法,修改.jsp檔案,將非select時指向executeUpdate,測試update,insert,
delete都成功實現
executeQuery方法代碼:
public ResultSet executeQuery(String sqlString)
{
rs=null;
try
{
conn=DriverManager.getConnection(connURL,userName,pwd);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlString);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
excuteUpdate方法代碼:
public int executeUpdate(String sqlString)
{
instructionCount=0;
try
{
conn=DriverManager.getConnection(connURL,userName,pwd);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlString);
instructionCount=1;
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return instructionCount;
}
新問題:在MySQL使用utf-8來支援全中文時,再次對支付串進行編解碼會破壞中文的輸入,
在插入和更新資料時,取消原來用GBK的new String 來編碼
作者