Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class InsertExample {
- public static void main(String[] args) {
- Connection connection = null;
- try {
- // load driver
- Class.forName("com.mysql.jdbc.Driver");
- // Tao ket noi
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
- // truy van
- String query = "INSERT INTO student_table(student_code, name, class, faculty) " + "VALUES(?, ?, ?, ?)";
- PreparedStatement preparedStmt = connection.prepareStatement(query);
- preparedStmt.setString(1, "SV2");
- preparedStmt.setString(2, "Tran Van A");
- preparedStmt.setString(3, "14T2");
- preparedStmt.setString(4, "Cong nghe thong tin");
- preparedStmt.executeUpdate();
- System.out.println("DONE!");
- } catch (ClassNotFoundException | SQLException e) {
- System.err.println(e);
- } finally {
- if (connection != null)
- try {
- connection.close();
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement