首頁| 新聞| 娛樂| 游戲| 科普| 文學| 編程| 系統| 數據庫| 建站| 學院| 產品| 網管| 維修| 辦公| 熱點
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"><% String sqlStatement=(String)session.getAttribute("sqlStatement"); if (sqlStatement == null) sqlStatement = ""; String message = (String) session.getAttribute("message"); if (message == null) message = "";%>
<form action="../servlet/test.SQLGatewayServlet" method="post"><b>SQL命令:</b><br><textarea name="sqlStatement" cols=60 rows=8><%=sqlStatement%></textarea><br><br><input type="submit" value="執行"></form>
<b>SQL命令執行結果:</b><br><table cellpadding="5" border="1"><%=message%></table>
package test;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;public class SQLGatewayServlet extends HttpServlet{ private Connection connection;
public void init() throws ServletException{ try{ Class.forName("org.gjt.mm.mysql.Driver"); String dbURL = "jdbc:mysql://localhost/murach"; String username = "root"; String passWord = ""; connection = DriverManager.getConnection (dbURL, username, password); } catch(ClassNotFoundException e){ System.out.println("找不到數據庫驅動程序."); } catch(SQLException e){ System.out.println("不能打開數據庫連接: " + e.getMessage()); }}
public void destroy() { try{ connection.close(); } catch(SQLException e){ System.out.println("不能關閉數據庫連接: " + e.getMessage()); }}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ doGet(request, response); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String sqlStatement = request.getParameter("sqlStatement"); String message = "";
try{ Statement statement = connection.createStatement(); sqlStatement = sqlStatement.trim(); String sqlType = sqlStatement.substring(0, 6);
if (sqlType.equalsIgnoreCase("select")){ ResultSet resultSet = statement.executeQuery(sqlStatement); // 構造一個String,其中包含HTML表格形式的結果集數據 message = SQLUtil.getHtmlRows(resultSet); }
else { int i = statement.executeUpdate(sqlStatement); if (i == 0) // 這是一個DDL命令 message = "<tr><td>" + "命令執行成功." + "</td></tr>"; else // 這是一個INSERT、UPDATE或DELETE命令 message = "<tr><td>" + "SQL命令執行成功。<br>" + "已更改" + i + " 行。" + "</td></tr>"; } statement.close(); }
catch(SQLException e){ message = "<tr><td>執行SQL命令時遇到錯誤:<br>" + e.getMessage() + "</tr></td>";}
HttpSession session = request.getSession(); session.setAttribute("message", message); session.setAttribute("sqlStatement", sqlStatement);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/sql/sql_gateway.jsp"); dispatcher.forward(request, response);
package test;import java.sql.*;public class SQLUtil{
public static synchronized String getHtmlRows(ResultSet results)throws SQLException{ StringBuffer htmlRows = new StringBuffer(); ResultSetMetaData metaData = results.getMetaData(); int columnCount = metaData.getColumnCount(); // 將記錄集中列的名稱作為HTML表格列的標題 htmlRows.append("<tr>"); for (int i = 1; i <= columnCount; i++) htmlRows.append("<td><b>" + metaData.getColumnName(i) + "</td>"); htmlRows.append("</tr>"); // 對于結果集中的每一行... while (results.next()){ htmlRows.append("<tr>"); // 將該行中的每一個列轉換成一個表格單元 for (int i = 1; i <= columnCount; i++) htmlRows.append("<td>" + results.getString(i) + "</td>"); } htmlRows.append("</tr>"); return htmlRows.toString();}
索泰發布一款GTX 1070 Mini迷
AMD新旗艦顯卡輕松干翻NVIDIA
索泰發布一款GTX 1070 Mini迷你版本:小機
芭蕾舞蹈表演,真實美到極致
下午茶時間,悠然自得的休憩
充斥這繁華奢靡氣息的城市迪拜風景圖片
從山間到田野再到大海美麗的自然風景圖片
肉食主義者的最愛美食烤肉圖片
夏日甜心草莓美食圖片
人逢知己千杯少,喝酒搞笑圖集
搞笑試卷,學生惡搞答題
新聞熱點
疑難解答
圖片精選
使用ASP建設私人搜索引擎
華為短消息中心的發展與應用
移動通信計費及客戶服務系統
移動客戶服務中心系統
網友關注