1

I'm new to Netbeans and now trying to make JDBC connectivity. I want to connect a MS Access database mis.accdb with my java file ShowData.java. The contents of 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...");
}
}
}

This code worked initially without Netbeans IDE and now when i tried to implement it in IDE, it shows me an error on the line rs=st.executeQuery("select * from student"); as

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

Please help me solve this issue and also guide me as to how to connect to the above specified MS Access Database mis.accdbin Netbeans. Thanks

Gaurav Gilalkar
  • 128
  • 2
  • 13

2 Answers2

3

The import java.beans.Statement is not the correct one. You probably meant java.sql.Statement.

After that change you can probably also remove the cast in this line:

st=(Statement) con.createStatement();
john16384
  • 7,800
  • 2
  • 30
  • 44
0

This is what I also faced This helped me solve it

I used

PreparedStatement st = null;

st = con.prepareStatement("THE QUERY");

ResultSet rs=st.executeQuery();

all this in the same event/function

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39