3

在 CentOS 服务器上运行 java 代码连接 mysql

我的代码是 -

String server = "", url = "", password = "", user = "", databasename = "";
    Connection con = null;
    try {
        server = "xxx.xxx.xxx.xxx";
        databasename = "xx";
        user = "user";
        password = "pass";
        Class.forName("com.mysql.jdbc.Driver");
        url = "jdbc:mysql://" + server + "/" + databasename + "";
        con = DriverManager.getConnection(url, user, password);
        System.out.println(server + " Database connection established");
        System.out.println(con);
    } catch (Exception e) {
        System.out.println("***************connection failed********************");
        e.printStackTrace();
    }

我的数据库在同一台服务器上运行。

我在我的本地系统和其他服务器中为同一数据库运行此代码,它运行良好,但它没有在我的数据库可用的同一服务器上运行。我得到了这个例外 -

    com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3361)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3295)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1852)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1975)
   at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2470)
   at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1669)
   at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3336)
   at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1979)
   at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
   at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:287)
   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
   at java.sql.DriverManager.getConnection(libgcj.so.10)
   at java.sql.DriverManager.getConnection(libgcj.so.10)
   at testthread.ConnectionFactory.getConnection(ConnectionFactory.java:28)
   at testthread.ConnectionFactory.main(ConnectionFactory.java:39)

有没有人帮我看看有什么问题。

4

1 回答 1

0

这个问题似乎与以下非常相似: Stumped SQL Exception for JDBC

尝试检查本地数据库服务器中的字符集配置/数据

于 2012-09-10T06:07:26.280 回答