在JDBCInsertByStatement.java
文件中,添加以下代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCInsertByStatement {
static String url = "jdbc:mysql://localhost:3306/edusys?useSSL=false";
static String user = "root";
static String password = "";
static Connection connection = null;
public static void main(String[] args) throws SQLException {
connection = DriverManager.getConnection(url, user, password);
PreparedStatement statement = connection.prepareStatement(
"insert into student(id,name,dept_name,tot_cred) values (?,?,?,?)");
statement.setString(1, "33123");
statement.setString(2, "Rong");
statement.setString(3, "Music");
statement.setInt(4, 67);
statement.execute();
statement.close();
connection.close();
}
}