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:
Code Java:
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.
Re: Please assist, issues with a JDBC connection
you must enclose the values inside single quote marks ( ' ), for example:
Code :
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.
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:
Code Java:
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();
}
}
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.