2014-12-03 42 views
0

我有這個代碼,它不會顯示我在我的數據庫中的東西。我創建了一個表並插入了一些數據。我知道這一切都很好,並且工作。但是我遇到的問題是它不會顯示任何數據,我不知道爲什麼......所以如果有人能幫我解決這個問題,那就太好了。只是不知道我做錯了什麼,或者如果我在這裏丟失的東西是我的代碼。我使用tomcat,如果這對servlet部分很重要。不會顯示我的數據庫信息在我的Java 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

首先,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,使用方法是這樣的:

 // Execute SQL query 
    stmt = connection.createStatement(); 
    String sql = "SELECT * FROM csc3610_Connection.Employees"; 
    ResultSet rs = stmt.executeQuery(sql); 
+0

確定我固定在 「SELECT * FROM csc3610_Connection.Employees」 ;部分,但它看起來像它沒有連接到我的數據庫,因爲我把一個out.println(「有連接」),並沒有顯示,所以即時通訊認爲它沒有連接到我的數據庫..你有什麼想法嗎? – tharealdeal15 2014-12-03 21:37:59

+0

你確定,你已經在Tomcat中部署了一個mysql連接器嗎?如果沒有,則下載並提取JAR文件,並將其複製到「$ CATALINA_HOME/lib」。這將使驅動程序可用於JDBC。 – Sal 2014-12-03 21:40:43

+0

我沒有那樣讓我試試 – tharealdeal15 2014-12-03 21:46:31