Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class InsertExample {
  4. public static void main(String[] args) {
  5. Connection connection = null;
  6. try {
  7. // load driver
  8. Class.forName("com.mysql.jdbc.Driver");
  9. // Tao ket noi
  10. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
  11. // truy van
  12. String query = "INSERT INTO student_table(student_code, name, class, faculty) " + "VALUES(?, ?, ?, ?)";
  13. PreparedStatement preparedStmt = connection.prepareStatement(query);
  14. preparedStmt.setString(1, "SV2");
  15. preparedStmt.setString(2, "Tran Van A");
  16. preparedStmt.setString(3, "14T2");
  17. preparedStmt.setString(4, "Cong nghe thong tin");
  18. preparedStmt.executeUpdate();
  19. System.out.println("DONE!");
  20. } catch (ClassNotFoundException | SQLException e) {
  21. System.err.println(e);
  22. } finally {
  23. if (connection != null)
  24. try {
  25. connection.close();
  26. } catch (SQLException e) {
  27. System.err.println(e);
  28. }
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement