Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please assist, issues with a JDBC connection

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Please assist, issues with a JDBC connection

    Any assistance would be greatly appreciated. I have a final project I am working on that is a simple webbot. After the webbot completes, the program finds the IP, and IP class, then inserts the information to an access DB using a JDBC connection. The programming is done, but when I execute the program, it fails on a DB import. The rest of the code runs without issue. Here is the offended code:

     
    statement.execute("INSERT INTO TheData (IPAddress, URL, IPClass) VALUES (" + address.getHostAddress() +", " + url1 + ", " + getClass(address) + ") ");

    The following error is returned:

    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Class A'.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc .java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java :7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java :3109)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcSt atement.java:337)
    at WebBot.dbWriter(WebBot.java:163)

    Any help would be greatly appreciated.


  2. #2

    Default Re: Please assist, issues with a JDBC connection

    you must enclose the values inside single quote marks ( ' ), for example:

    String sql = "INSERT INTO TheData (IPAddress, URL, IPClass) VALUES ('" + address.getHostAddress()  + "')";

    However, this is not a good practice. Instead, you should use PreparedStatement with setXXX() method to set value for query's parameters.

  3. The Following User Says Thank You to nambill For This Useful Post:

    gmetz (December 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please assist, issues with a JDBC connection

    Thank you, that has worked. Sadly it's only writing one entry into the DB. Here is the class that we are working with:

    	   public static void dbWriter() {
    			try {
    				Connection conn = getConnection();
    				Statement statement = conn.createStatement();
    				InetAddress address = null;
    				String url1 = null;
     
    				for(String url : fullSet)
    					 url1 = url.substring(url.indexOf("//") + 2);
    				try {
    					address = InetAddress.getByName(url1);
    					statement.execute("INSERT INTO TheData (IPAddress, URL, IPClass) VALUES (' " + address.getHostAddress() + " ',' " + url1 + " ' , ' " + getClass(address) + " ') ");
    				} catch (UnknownHostException e) {
    					System.out.println(url1);
    					e.printStackTrace();
    				}catch (SQLException ex) {
    					while (ex != null) {
    						ex.printStackTrace();
    						ex = ex.getNextException();
    					}
    				}
    				statement.close();
    				conn.close();
    			} catch (SQLException ex) {
    				while (ex != null) {
    					ex.printStackTrace();
    					ex = ex.getNextException();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}

  5. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please assist, issues with a JDBC connection

    Try the following

    1. Give Debug prints Size of full set

    2. Try to add the for loop contents in a flower braces.

    Example

    for()
    {

    }

    3. check the statement and conn values.

Similar Threads

  1. JDBC Too Many Connection Error
    By sajithgsm in forum JDBC & Databases
    Replies: 1
    Last Post: February 22nd, 2012, 04:08 AM
  2. [SOLVED] JDBC Connection from Single Computer
    By Max Avion in forum JDBC & Databases
    Replies: 4
    Last Post: November 17th, 2011, 11:41 AM
  3. jdbc connection problem
    By sny in forum JDBC & Databases
    Replies: 11
    Last Post: January 6th, 2011, 03:39 AM
  4. JDBC connection error
    By nrao in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 11th, 2010, 11:43 AM
  5. jdbc connection with phpmyadmin
    By anand_fevi in forum JDBC & Databases
    Replies: 1
    Last Post: May 13th, 2010, 02:26 AM