我創建了一個簡單的Java類訪問SQL數據庫的正常工作 下面的代碼:神祕的SQL訪問行爲
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Query2 {
public static void main (String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/riconoscimento?"
+ "user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT * FROM utenti; ");
while (rs.next()) {
String passwordToRead = rs.getString("password");
String usernameToRead = rs.getString("username");
System.out.println(passwordToRead);
System.out.println(usernameToRead);
}
conn.close();
} catch (Exception e) {
System.err.println("Database Access Error");
System.err.println(e.getMessage());
}
}
}
在JSP頁面中相同的代碼不工作,問題是未執行try-catch塊,我不知道它是如何可能 下面的代碼:
<%@ page language ="java" import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>I dati inseriti per l'autenticazione sono:</title>
</head>
<body>
<h1>
<%
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/riconoscimento?"
+ "user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT * FROM utenti; ");
while (rs.next()) {
String passwordToRead = rs.getString("password");
String usernameToRead = rs.getString("username");
out.println(passwordToRead);
out.println(usernameToRead);
}
conn.close();
} catch (Exception e) {
System.err.println("Database Access Error");
System.err.println(e.getMessage());
}
%>
</h1>
</body>
</html>
你有沒有關閉所有的Url打開標籤?像在ur jsp 本身沒有關閉。 而且是jsp得到編譯,你看到的輸出是什麼? – 2012-01-12 09:27:10
忘了這裏,但現在複製的.jsp網頁.. 實在無法理解 – Franky 2012-01-12 09:28:44
增量部署的http://本地主機:8080/WebApplication1 完成增量HTTP的分佈://本地主機:8080/WebApplication1 運行部署: 瀏覽:HTTP://本地主機:8080/WebApplication1/index.jsp的 運行顯示的瀏覽器: 運行: BUILD SUCCESSFUL(總時間:4秒) – Franky 2012-01-12 09:31:34