我是Netbeans的新手,现在尝试建立JDBC连接。我想将MS Access数据库mis.accdb
与我的java文件ShowData.java
连接起来。 ShowData.java
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中实现它时,它在行rs=st.executeQuery("select * from student");
上显示错误为
cannot find symbol
symbol: method executeQuery(String)
location: variable st of type Statement
请帮我解决这个问题,并指导我如何在Netbeans中连接到上面指定的MS Access数据库mis.accdb
。感谢
答案 0 :(得分:2)
导入java.beans.Statement
不正确。你可能意味着java.sql.Statement
。
在此更改之后,您可以删除此行中的演员:
st=(Statement) con.createStatement();
答案 1 :(得分:0)
这也是我遇到的 这帮我解决了
我用过
PreparedStatement st = null;
st = con.prepareStatement("THE QUERY");
ResultSet rs=st.executeQuery();
所有这些都在同一个事件/函数中