Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. /*
  2. * A MySQL DROP DATABASE 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 database in given database...");
  33. stmt = conn.createStatement();
  34. String sql = "DROP DATABASE demo_databas";
  35. stmt.executeUpdate(sql);
  36. } catch (SQLException se) {
  37. //Handle errors for JDBC
  38. se.printStackTrace();
  39. } catch (Exception e) {
  40. //Handle errors for Class.forName
  41. e.printStackTrace();
  42. } finally {
  43. //finally block used to close resources
  44. try {
  45. if (stmt != null) {
  46. conn.close();
  47. }
  48. } catch (SQLException se) {
  49. }// do nothing
  50. try {
  51. if (conn != null) {
  52. conn.close();
  53. }
  54. } catch (SQLException se) {
  55. se.printStackTrace();
  56. }//end finally try
  57. }//end try
  58. System.out.println("Goodbye!");
  59. }//end main
  60. }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement