我有这个代码,它不会显示我在我的数据库中的东西。我创建了一个表并插入了一些数据。我知道一切都很好并且有效。但我遇到的问题是它不会显示任何数据,我不知道为什么......所以,如果有人可以帮助我,那将是伟大的。只是不承担我做错了什么或如果我错过了什么在这里是我的代码。我使用tomcat,如果这对servlet部分很重要。
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
}
}
答案 0 :(得分:0)
首先,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);