Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. /*
  2. * A MySQL UPDATE example using Java
  3. * @author mysqlcommands.com
  4. */
  5.  
  6. //STEP 1. Import required packages
  7. import java.sql.*;
  8.  
  9. public class JDBCExample {
  10. // JDBC driver name and database URL
  11.  
  12. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  13. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  14.  
  15. // Database credentials
  16. static final String USER = "username";
  17. static final String PASS = "password";
  18.  
  19. public static void main(String[] args) {
  20. Connection conn = null;
  21. Statement stmt = null;
  22. try {
  23. //STEP 2: Register JDBC driver
  24. Class.forName("com.mysql.jdbc.Driver");
  25.  
  26. //STEP 3: Open a connection
  27. System.out.println("Connecting to a selected database...");
  28. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  29. System.out.println("Connected database successfully...");
  30.  
  31. //STEP 4: Execute a query
  32. System.out.println("Deleting table in given database...");
  33. stmt = conn.createStatement();
  34.  
  35. String sql = "DROP TABLE REGISTRATION ";
  36.  
  37. stmt.executeUpdate(sql);
  38. System.out.println("Table deleted in given database...");
  39. } catch (SQLException se) {
  40. //Handle errors for JDBC
  41. se.printStackTrace();
  42. } catch (Exception e) {
  43. //Handle errors for Class.forName
  44. e.printStackTrace();
  45. } finally {
  46. //finally block used to close resources
  47. try {
  48. if (stmt != null) {
  49. conn.close();
  50. }
  51. } catch (SQLException se) {
  52. }// do nothing
  53. try {
  54. if (conn != null) {
  55. conn.close();
  56. }
  57. } catch (SQLException se) {
  58. se.printStackTrace();
  59. }//end finally try
  60. }//end try
  61. System.out.println("Goodbye!");
  62. }//end main
  63. }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement