简体   繁体   中英

Cannot Retrieve Data From Access Database

I am trying to create an java web application. In that application i create a servlet to retrieve data from MS-Access database. I did this so many times in our laboratory but now i cant. I dont know what is the reason. Here is my processRequest method:

ProcessRequest

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ClassNotFoundException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        String des=request.getParameter("desc");
        Class.forName("sun.jdbc.odbc.JdbcOdbcConnection");
        Connection con=DriverManager.getConnection("jdbc:odbc:wsds","","");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from Table1 where wsdesc="+des);
        while(rs.next())
        {
            String description=rs.getString("wsdesc");
            String url=rs.getString("url");
            out.println(description);
            out.println(url);
        }

    } finally {   
        out.close();
    }
}

I use Netbeans 7.0.1 IDE. I create a datasource wsds. I check the table column names are correct. Anyone can help me??

change

ResultSet rs=st.executeQuery("select * from Table1 where wsdesc="+des);

to

ResultSet rs=st.executeQuery("select * from Table1 where wsdesc='"+des+"'");

Varchar datatypes should be enclosed with ' '

Instead of Statement use PreparedStatement

PreparedStatement pt=con.prepareStatement("select * from Table1 where wsdesc=?");
pt.setString(1,desc);
ResultSet rs=pt.executeQuery();

将驱动程序名称更新为

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

尝试添加引号

ResultSet rs=st.executeQuery("select * from Table1 where wsdesc='"+des+"'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM