Results 1 to 3 of 3
  1. #1
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default SQL exception error in JDBC conection with servlet+ jsp

    my code is like this




    i could print from database to my user interface by select command but when i want to update to database through user interface i am getting this error


    SQL exception: General error!!!!

    i check this with MS accesss ORACLE and ORACLE Exprress as well!!!
    Java Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.io.*;
    import java.sql.*;
    public class editServlet extends HttpServlet{
    private Connection lCon;
    	public void init(){
    		try{
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			lCon=DriverManager.getConnection("jdbc:odbc:records","test","test");
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    
    	public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)throws ServletException,IOException {
    		String lId=pRequest.getParameter("id");
    		String lFirstname=pRequest.getParameter("firstname");
    		String lLastname=pRequest.getParameter("lastname");
    		String lAddress=pRequest.getParameter("address");
    		String lMessage="";
    		List lRecordsList=null;
    		try{
    			if (lId != null ){
    				//edit	
    				if(lCon == null){
    					init();
    				}		
    				PreparedStatement lPreparedSatement = lCon.prepareStatement("update Personal set firstname=? , lastname=?,address=? where id=?");
    				lPreparedSatement.setString(1,lFirstname);
    				lPreparedSatement.setString(2,lLastname);
    				lPreparedSatement.setString(3,lAddress);
    				lPreparedSatement.setString(4,lId);
    				lPreparedSatement.executeUpdate();
    
    				lMessage="Record Saved Successfully.";
    
    				lPreparedSatement.close();
    			
    			}else{
    				lMessage="Id attribute was null.";
    			}
    
    
    			//Select
    			if(lCon == null){
    				init();
    			}		
    			Statement lStatement = lCon.createStatement();
    			ResultSet lResultSet = lStatement.executeQuery("Select * from Personal");		 
    
    			lRecordsList = new ArrayList();
    
    			while(lResultSet.next()){
    				Map lMap = new HashMap();
    	
    				lMap.put("id",lResultSet.getString("id"));
    				lMap.put("firstname",lResultSet.getString("firstname"));
    				lMap.put("lastname",lResultSet.getString("lastname"));
    				lMap.put("address",lResultSet.getString("address"));
    
    				lRecordsList.add(lMap);
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    			lMessage="There was some problem, while saving record.";
    		}finally{
    			try{lCon.close();}catch(Exception e){e.printStackTrace();}
    		}
    
    
    		RequestDispatcher lRequestDispatcher = pRequest.getRequestDispatcher("index.jsp");
    		pRequest.setAttribute("Records"	,lRecordsList);
    		pRequest.setAttribute("Message",lMessage);
    		lRequestDispatcher .forward(pRequest,pResponse);
    	}
    
    
    	public void destroy(){
    		if(lCon != null){
    			try{lCon.close();}catch(Exception e){e.printStackTrace();}
    		}		
    	}
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    the ODBC JDBC driver probably does not have prepared statement support implemented.

    That jdbc-odbc bridge is pretty old, and probably by design basic, most ODBC things used to be assumed to be read-only, such as exporting an excel file as an ODBC data source.

    Try to use the ghetto Statement, with of course proper handling for sql injection and escaping of special characters and quotes. and if that is still not working, then for some reason this ODBC connection is behaving as if it is read only.

    would it be possible to use a not-odbc connection? you mentioned you tried this with oracle and mssql. Both of these databases do come with their own JDBC drivers, connecting to the database using their JDBC implementation might work better.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    As a basic rule, never use the JDBC-ODBC bridge. It's rubbish.
    I can't offhand think of a db that does not come with it's own proper driver, so use it.

    You'll also possibly get a better exception thrown if there is a problem.

Similar Threads

  1. jsp conection please help me
    By tvareddys.reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-29-2008, 11:57 AM
  2. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  3. Problem in Servlet with JDBC
    By *New Programer* in forum Java Servlet
    Replies: 2
    Last Post: 12-20-2007, 09:15 AM
  4. JDBC - Exception handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:00 PM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •