Reputation: 55
I'm trying to connect to a mysql database using Connector/J but get the same message no matter what code I use. I can connect to it manually with the mysql command on debian just fine.
The error is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
The latest code is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class E{
public void connect(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:mysql://192.168.1.3:3306/dbname?user=dbuser&password=dbpw");
}catch( Exception e){
System.out.printf("Something went wrong: "+e.getMessage());
}
}
It fails at the getConnection call. I used print statements to figure this out as NetBeans doesn't have a problem with this (but when I put in a bad host and run it from NetBeans it doesn't complain either).
I get the exact same problem with DataSource
import java.sql.*;
import javax.sql.*;
...
com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
= new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
ds.setServerName("192.168.1.3");
ds.setPortNumber(3306);
ds.setDatabaseName("dbname");
ds.setUser("dbuser");
ds.setPassword("dbpw");
conn = ds.getConnection();
...
Something is working because if I put in a bad password I get an access denied message.
I'm using mysql-connector-java-5.1.22-bin.jar
and had the same problem with 5.1.21.
Upvotes: 1
Views: 1513
Reputation: 55
I found the following: Stumped SQL Exception for JDBC
In a nutshell, the server my.cnf file needs:
character_set_server=utf8
collation_server=utf8_general_ci
It also said the GCJ (gnu java compiler) is dicey, but I didn't replace it
Upvotes: 0
Reputation: 4847
Your getConnection
statement should be
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3:3306/dbname", "dbuser", "dbpw");
Refer this example
Upvotes: 0
Reputation: 8606
These might help you:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
Problem with connecting to MySQL server
Upvotes: 1