Java JDBC仅将空值插入MySQL表

问题描述 投票:0回答:1

我正在尝试通过Java中的JDBC连接将数据插入MySQL表中。我可以建立连接。但是,我的代码仅为所有列插入空值。我在准备好的语句中使用伪数据,希望看到非空值。

关于可能发生的事情的任何想法或指导?我是JBDC和Java的新手。

这是我创建表的方式:

    drop table market_data.abc;
create table market_data.abc(
`v` int,
`w` int
)
;

这是我创建连接的方式:

    public static Connection ConnectToDB() throws Exception {

        // register driver
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

        // create database url
        mysqlUrl = "jdbc:mysql://localhost:3306/" + databaseName + "?characterEncoding=" + encoding
                + "&&useUnicode=" + unicode; 

        // create connection
        Connection con = DriverManager.getConnection(mysqlUrl, userName, password);

        // print connection status message
        System.out.println("Connection established......");

        // return database connection
        return con;
    }

在这里插入数据:

void insertRecords() {
        try {

            // Establish Connection
            Connection con = cc.ConnectToDB();

            // Set the query structure
            String insertString = "INSERT INTO abc values (" + "v=?,w=?)";

            // Set prepared statement
            PreparedStatement statement = con.prepareStatement(insertString);

            // set values to variables
            int v1 = 2;
            int w1 = 3;

            // set values to statement
            statement.setInt(1, v1);
            statement.setInt(2, w1);

            // execute the insert operation; 
            statement.executeUpdate();

            // print status message for record insertion
            System.out.println("New Records inserted.....");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我从CLI执行Select *时看到的内容:


 v    | w    |
+------+------+
| NULL | NULL |
| NULL | NULL |
+------+------+

这是我的MySQL字符集变量:

show variables like 'char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8mb4                                                   |
| character_set_connection | utf8mb4                                                   |
| character_set_database   | utf8mb4                                                   |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8mb4                                                   |
| character_set_server     | utf8mb4                                                   |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-8.0.20-macos10.15-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+

java mysql jdbc
1个回答
0
投票

您是否只尝试使用.execute()而不是.executeUpdate?您未更新,正在插入。

© www.soinside.com 2019 - 2024. All rights reserved.