我正在尝试与MySQL数据库建立连接,而我正在使用Tomcat v6.0。 Java中的源代码是:
ConnectionManager.java
package admin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private static ConnectionManager instance = null;
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String CONN_STRING =
"jdbc:mysql://localhost/sondazh";
private Connection conn = null;
private ConnectionManager() {
}
public static ConnectionManager getInstance() {
if (instance == null) {
instance = new ConnectionManager();
}
return instance;
}
private boolean openConnection() throws ClassNotFoundException {
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public Connection getConnection() {
if (conn == null) {
try {
if (openConnection()) {
return conn;
} else {
return null;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return conn;
}
public void close() {
try {
conn.close();
conn = null;
} catch (Exception e) {
}
}
}
DatabaseObject.java
package admin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
public abstract class DatabaseObject {
private static Connection conn = ConnectionManager.getInstance()
.getConnection();
public static ResultSet query(String sql) {
try {
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
return rs;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
System.out.println(e.getMessage());
return null;
}
}
public static boolean update(String sql){
try {
PreparedStatement stmt = conn.prepareStatement(sql);
int affected = stmt.executeUpdate();
if(affected == 0){
return false;
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
public static int insert(String sql){
try {
Statement stmt = conn.createStatement();
int affected = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
return affected;
} catch (Exception e) {
return 0;
}
}
}
我还为数据库中的所有实体创建了getter和setter 但是当我跑步时,它显示了一个错误:
HTTP状态404 - 输入状态报告
消息
说明请求的资源不可用。
Apache Tomcat / 6.0.36
我想显示来自用户实体的一些数据:
test.jsp
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="admin.User"%>
<%@page import="admin.ConnectionManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> test </title>
</head>
<body>
<div>
<table>
<%
ResultSet rs = User.loadAll();
while (rs.next()) {
%>
<tr>
<TH>ID</TH>
<TH>Name</TH>
<TH>Surname</TH>
<TH>Email</TH>
</tr>
<tr>
<td> <%= rs.getInt(1) %> </td>
<td> <%= rs.getString(2) %> </td>
<td> <%= rs.getString(3) %> </td>
<td> <%= rs.getString(4) %> </td>
</tr>
</table>
</div>
<%
}
%>
</body>
</html>
这是我尝试运行的test.jsp,但由于前面提到的错误,我无法得到我想要的结果。 Tomcat正在运行。我的jsp代码好吗?
答案 0 :(得分:0)
404与数据库无关。 Web服务器无法找到URL标识的资源。您的问题在于URL或Web服务器配置。
答案 1 :(得分:0)
localhost:8080/Sondazh/WEB-INF/test.jsp <<--- JSP is under WEB-INF here.
您无法直接访问此内容。它不是标准的Servlet / JSP规范。容器在WEB-INF中查找类文件,如果在此目录中请求JSP,则会返回错误404.需要保护Http访问的资源放在WEB-INF下。 To To访问JSP,将其保留在WEB-INF之外。如果部署在Tomcat中,您可以将JSP放在\apache-tomcat-version\webapps\Sondazh\test.jsp
下,并使用localhost:8080/Sondazh/test.jsp
命中它。