无法在MySQL中插入信息

时间:2018-03-12 13:04:20

标签: java mysql jdbc permissions

我试图在数据库中本地插入信息,其中I程序是用Java编写的,但我无法做到。我的猜测是,它与用户权限有某种关系,即使我不知道除了我已有的东西之外应该做些什么。我的操作系统是Ubuntu 14.04.05 LTS。我的行动如下:

我打开一个命令行界面窗口并通过

进入MySQL
mysql -u root -p

然后我输入root密码并进入。

我将数据设置为必须与

一起使用的数据库
use_data;

我用

创建了一个新用户
CREATE USER 'user'@'localhost' IDENTIFIED BY 'passw';

无论使用何种IP,我都会为用户提供写入信息所需的所有权限:

GRANT USAGE ON * . * TO 'user'@'localhost' IDENTIFIED BY 'passw' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

GRANT USAGE ON * . * TO 'user'@'%' IDENTIFIED BY 'passw' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

GRANT USAGE ON * . * TO 'user'@'linux' IDENTIFIED BY 'passw' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

GRANT ALL PRIVILEGES ON `data` . * TO 'user'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON `data` . * TO 'user'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON `data` . * TO 'user'@'linux' WITH GRANT OPTION;

然而,当我尝试访问数据库以插入信息时,我得到以下异常:

Exception in thread "Thread-3" java.lang.StackOverflowError
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)

有人能告诉我,我的错误是不能获得我的Java代码和数据库之间的连接吗?当我尝试访问数据库时,抛出异常的代码部分位于getConnection方法中。

@Override
    public void openDBConnection() throws Exception {
        //this method establish a database connection
        try {
            // This will load the MySQL driver, each DB has its own driver
            Class.forName(driver);

            String connection = "jdbc:mysql://" + host + "/" + dbname + "?"
                    + "user=" + user + "&password=" + passwd + "&useSSL=false"; 
            System.out.println(connection);

            // Setup the connection with the DB
            connect = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbname + "?"
                    + "user=" + user + "&password=" + passwd + "&useSSL=false");

        } catch (ClassNotFoundException | SQLException e) {
            throw e;
        } finally {
        }
    }

1 个答案:

答案 0 :(得分:0)

好吧,您应该尝试检查de 驱动程序值(对于行Class.forName(driver)),并确保您正在访问正确的类。

此外,错误显示您的堆栈中没有足够的内存,因此您可以更改 JVM堆栈大小以增加此值(您可以使用{ {1}} jvm参数)