import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertUser {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database_name";
String username = "your_username";
String password = "your_password";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "root");
preparedStatement.setString(2, "123");
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("User 'root' with password '123' inserted successfully");
} else {
System.out.println("Failed to insert user");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
编写JDBC程序实现PreparedStatement插入到用户数据库user中,username是root,password是123的记录
最新推荐文章于 2024-09-01 13:09:09 发布