2017-02-14 54 views
0

我是Netbeans的新手,現在正試圖建立JDBC連接。我想連接一個MS Access數據庫mis.accdb與我的java文件ShowData.java。的ShowData.java找不到符號executeQuery

import java.beans.Statement; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class ShowData extends HttpServlet 
{ 
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException 
{ 
response.setContentType("text/html"); 
PrintWriter out=response.getWriter(); 
Connection con=null; 
Statement st=null; 
ResultSet rs=null; 
try{ 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    try 
    { 
     con=DriverManager.getConnection("jdbc:odbc:mis"); 
    } catch (SQLException ex) 
    { 
     Logger.getLogger(ShowData.class.getName()).log(Level.SEVERE, null, ex); 
    } 
st=(Statement) con.createStatement(); 
rs=st.executeQuery("select * from student"); 
out.println("<table border='1'><tr><th>Student ID</th><th>Student Name</th><th>Branch</th></tr>"); 
while(rs.next()) 
{ 
int sid=rs.getInt("StudId"); 
String snm=rs.getString("StudName"); 
String br=rs.getString("Branch"); 
out.println("<tr>"); 
out.println("<td>"+sid+"</td>"); 
out.println("<td>"+snm+"</td>"); 
out.println("<td>"+br+"</td>"); 
} 
} 
catch(ClassNotFoundException e) 
{ 
out.println("Driver Loading Failed..."); 
} 
catch(SQLException e) 
{ 
out.println("Please Check SQL Query..."); 
} 
} 
} 

內容這段代碼最初工作沒有NetBeans IDE和現在當我試圖實現它的IDE,它顯示我作爲

cannot find symbol 
symbol: method executeQuery(String) 
location: variable st of type Statement 

請幫我解決就行了rs=st.executeQuery("select * from student");錯誤這個問題,並且還指導我如何連接到Netbeans中的上述指定的MS Access數據庫mis.accdb。謝謝

+2

你將需要爲這個JDBC驅動程序,嘗試尋找在這篇文章可能會有所幫助:HTTP://計算器。 com/questions/20007353/connect-to-access-database-in-java-using-netbeans – Aaron

+0

非常感謝。這工作...! –

回答

0

進口java.beans.Statement是不正確的。你可能意思是java.sql.Statement

這種改變後,您也許可以同時刪除投在這條線:

st=(Statement) con.createStatement(); 
+0

試過這個。仍然沒有輸出。只是一個空白的屏幕。 –