我有这段代码,它不会显示我数据库中的内容。我创建了一个表并插入了一些数据。我知道一切都是好的并且有效的。但我遇到的问题是它不会显示任何数据,我不知道为什么......所以如果有人可以帮助我那就太好了。只是不明白我做错了什么,或者如果我遗漏了一些东西,这是我的代码。如果这对 servlet 部分很重要,我会使用 tomcat。
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class test extends HttpServlet{
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://localhost/csc3610_Connection";
// Database credentials
static final String USER = "root";
static final String PASS = "";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n");
Connection conn=null;
Statement stmt=null;
try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// Execute SQL query
stmt = conn.createStatement();
String sql;
sql = "SELECT FROM csc3610_Connection.Employees";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
out.println("ID: " + id + "<br>");
out.println(", Age: " + age + "<br>");
out.println(", First: " + first + "<br>");
out.println(", Last: " + last + "<br>");
}
out.println("</body></html>");
// Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
} //end try
}
}
最佳答案
首先,SELECT 语句是错误的。你必须使用
sql = "SELECT * FROM csc3610_Connection.Employees";
然后请重构检索数据库连接的代码。您不应该对每个请求都打开一个新连接。此外,Java 中的类名应以大写字母开头。看看Java Code Conventions
例如:
private Connection connection = null;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
// Load the MySQL driver
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(
DB_URL, "root", "");
}
catch (ClassNotFoundException e)
{
throw new UnavailableException(this, "Couldn't load database driver");
}
catch (SQLException e)
{
throw new UnavailableException(this, "Couldn't get db connection");
}
}
然后在你的 doGet-Method 中使用如下内容:
// Execute SQL query
stmt = connection.createStatement();
String sql = "SELECT * FROM csc3610_Connection.Employees";
ResultSet rs = stmt.executeQuery(sql);
关于java - 不会在我的 java servlet 中显示我的数据库信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281872/