I am trying to insert data into a MySQL table via a JDBC connection in Java. I can establish a connection. However, my code only inserts null values for all my columns. I am using dummy data in my prepared statement and expect to see non-null values.
Any thoughts or guidance as to what may be going on? I am new to JBDC and Java.
Here is how I create my table:
drop table market_data.abc;
create table market_data.abc(
`v` int,
`w` int
)
;
Here is how I create a connection:
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;
}
Here is where I insert data:
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();
}
}
Here is what I see when I do a Select * from the CLI:
v | w |
+------+------+
| NULL | NULL |
| NULL | NULL |
+------+------+
Here are my MySQL character set variables:
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/ |
+--------------------------+-----------------------------------------------------------+
try to replace insertString value with this one value
String insertString = "INSERT INTO abc values (v=?,w=?)";
and take care that INSERT statement has tow writing types.
first type is:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
second one is:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.