Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. /*
  2. * A MySQL RENAME TABLE example using Java
  3. * @author mysqlcommands.com
  4. */
  5.  
  6. //STEP 1. Import required packages
  7. import java.sql.*;
  8. import java.util.List;
  9.  
  10. public class JDBCExample {
  11. // JDBC driver name and database URL
  12.  
  13. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  14. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  15.  
  16. // Database credentials
  17. static final String USER = "username";
  18. static final String PASS = "password";
  19.  
  20. public static void main(String[] args) {
  21. Connection conn = null;
  22. Statement stmt = null;
  23. try {
  24. //STEP 2: Register JDBC driver
  25. Class.forName("com.mysql.jdbc.Driver");
  26.  
  27. //STEP 3: Open a connection
  28. System.out.println("Connecting to a selected database...");
  29. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  30. System.out.println("Connected database successfully...");
  31.  
  32. //STEP 4: Execute a query
  33. String sql = "RENAME TABLE tb1 TO tb2";
  34. stmt.executeUpdate(sql);
  35. System.out.println("renaming table successfully...");
  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