Guest User

Untitled

a guest
Jan 10th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MigracionBaseDatos {
  4. // Driver JDBC y URL base de datos
  5. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6. static final String DB_URL = "";
  7.  
  8. // Credenciales base datos
  9. static final String USER = "";
  10. static final String PASS = "";
  11.  
  12. // Crear las tablas MIGRACION_EXPEDIENTE Y MIGRACION_DOCUMENTO si no estuviesen creadas ya en la base de datos
  13. private void crearTablas() {
  14. String migracionExpediente = "CREATE TABLE MIGRACION_EXPEDIENTE ("
  15. + "SID INT(64) NOT NULL AUTO_INCREMENT,"
  16. + "SID_EXPEDIENTE INT(64) NOT NULL,"
  17. + "FECHA_ULTIMA_ACTUALIZACION TIMESTAMP(6),"
  18. + "RESULTADO VARCHAR(2),"
  19. + "SID_GEDE VARCHAR(2),"
  20. + "ERROR INT(64))";
  21. try {
  22. Class.forName(jdbcDriver);
  23. con = DriverManager.getConnection(dbAddress + dbName, userName, password);
  24. statement = con.createStatement();
  25. //The next line has the issue
  26. statement.executeUpdate(myTableName);
  27. System.out.println("Table Created");
  28. }
  29. catch (SQLException e ) {
  30. System.out.println("An error has occurred on Table Creation");
  31. }
  32. catch (ClassNotFoundException e) {
  33. System.out.println("An Mysql drivers were not found");
  34. }
  35. }
  36.  
  37. public static void main(String[] args) {
  38. Connection conn = null;
  39. Statement stmt = null;
  40. try{
  41. // Registrar driver JSBC
  42. Class.forName("com.mysql.jdbc.Driver");
  43.  
  44. //STEP 3: Open a connection
  45. System.out.println("Connecting to a selected database...");
  46. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  47. System.out.println("Connected database successfully...");
  48.  
  49. //STEP 4: Execute a query
  50. System.out.println("Creating table in given database...");
  51. stmt = conn.createStatement();
  52.  
  53. String sql = "CREATE TABLE REGISTRATION " +
  54. "(id INTEGER not NULL, " +
  55. " first VARCHAR(255), " +
  56. " last VARCHAR(255), " +
  57. " age INTEGER, " +
  58. " PRIMARY KEY ( id ))";
  59.  
  60. stmt.executeUpdate(sql);
  61. System.out.println("Created table in given database...");
  62. }catch(SQLException se){
  63. //Handle errors for JDBC
  64. se.printStackTrace();
  65. }catch(Exception e){
  66. //Handle errors for Class.forName
  67. e.printStackTrace();
  68. }finally{
  69. //finally block used to close resources
  70. try{
  71. if(stmt!=null)
  72. conn.close();
  73. }catch(SQLException se){
  74. }// do nothing
  75. try{
  76. if(conn!=null)
  77. conn.close();
  78. }catch(SQLException se){
  79. se.printStackTrace();
  80. }//end finally try
  81. }//end try
  82. System.out.println("Goodbye!");
  83. }//end main
  84. }//end JDBCExample
Add Comment
Please, Sign In to add comment