Guest User

Untitled

a guest
Nov 15th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package BosqueJDBC;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class ClasePrincipal {
  9.  
  10. public static void main(String[] args) {
  11.  
  12.  
  13.  
  14.  
  15. }
  16.  
  17. public void CrearDDBB() {
  18. // driver jdbc
  19. try {
  20. Class.forName("com.mysql.cj.jdbc.Driver");
  21. } catch (ClassNotFoundException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25.  
  26. // Credenciales
  27. final String CREATE_DATABASE = "CREATE DATABASE bosque";
  28. final String CREATE_USER = "root";
  29. final String CREATE_PASS = "_HidrA3773_";
  30. final String URL_DB = "jdbc:mysql://localhost:3306";
  31. Connection con = null;
  32. Statement stmt = null;
  33.  
  34. // Java JDBC driver nombre y url bbdd
  35. try {
  36. System.out.println("Creando la bd...");
  37. con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
  38. stmt = con.createStatement();
  39. stmt.executeUpdate(CREATE_DATABASE);
  40.  
  41. System.out.println("Base de datos creada");
  42.  
  43. } catch (SQLException e) {
  44. // TODO Auto-generated catch block
  45. e.printStackTrace();
  46. }
  47.  
  48. }
  49.  
  50. public void CrearTablas() {
  51. // driver jdbc
  52. try {
  53. Class.forName("com.mysql.cj.jdbc.Driver");
  54. } catch (ClassNotFoundException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58.  
  59. // Credenciales
  60. String CREATE_TABLE_PLANTA = "CREATE TABLE planta";
  61. String CREATE_TABLE_ARBUSTO = "CREATE TABLE arbusto";
  62. String CREATE_TABLE_ARBOL = "CREATE TABLE arbol";
  63. final String CREATE_USER = "root";
  64. final String CREATE_PASS = "_HidrA3773_";
  65. final String URL_DB = "jdbc:mysql://localhost:3306/bosque";
  66. Connection con = null;
  67. Statement stmt = null;
  68.  
  69. CREATE_TABLE_PLANTA = "CREATE TABLE planta" + "(id INTEGER not NULL AUTO_INCREMENT, " + " nombre VARCHAR(50), "
  70. + " PRIMARY KEY ( id ))";
  71.  
  72. CREATE_TABLE_ARBUSTO = "CREATE TABLE arbusto " + "(id INTEGER not NULL AUTO_INCREMENT, "
  73. + " flores VARCHAR(50), " + " PRIMARY KEY ( id ))";
  74.  
  75. CREATE_TABLE_ARBOL = "CREATE TABLE arbol " + "(id INTEGER not NULL AUTO_INCREMENT, " + " height FLOAT, "
  76. + " circunference FLOAT, " + " foliageDensity INTEGER, " + " treeTrunkColor VARCHAR(50), "
  77. + " PRIMARY KEY ( id ))";
  78. ;
  79.  
  80. // Java JDBC driver nombre y url bbdd
  81. try {
  82. System.out.println("Creando tablas...");
  83. con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
  84. stmt = con.createStatement();
  85. stmt.executeUpdate(CREATE_TABLE_PLANTA);
  86. stmt.executeUpdate(CREATE_TABLE_ARBUSTO);
  87. stmt.executeUpdate(CREATE_TABLE_ARBOL);
  88.  
  89. System.out.println("Tablas creadas");
  90.  
  91. } catch (SQLException e) {
  92. // TODO Auto-generated catch block
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97. public void InsertarTablas() {
  98. try {
  99. Class.forName("com.mysql.cj.jdbc.Driver");
  100. } catch (ClassNotFoundException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104.  
  105. final String CREATE_USER = "root";
  106. final String CREATE_PASS = "_HidrA3773_";
  107. final String URL_DB = "jdbc:mysql://localhost:3306/bosque";
  108. Connection con = null;
  109. Statement stmt = null;
  110.  
  111. String INSERT_INTO_PLANTAS = "INSERT INTO planta (nombre) " +
  112. "VALUES ('Plantas arbustivas')";
  113. String INSERT_INTO_ARBUSTO = "INSERT INTO arbusto (flores)" +
  114. "VALUES ('Buddleja alternifolia')";
  115. String INSERT_INTO_ARBOL = "INSERT INTO arbol (height, circunference, foliageDensity,treeTrunkColor)" +
  116. "VALUES (34.5,20.6,15,'Marron')";
  117.  
  118.  
  119. try {
  120. System.out.println("Insertando datos...");
  121. con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
  122. stmt = con.createStatement();
  123. stmt.executeUpdate(INSERT_INTO_PLANTAS);
  124. stmt.executeUpdate(INSERT_INTO_ARBUSTO);
  125. stmt.executeUpdate(INSERT_INTO_ARBOL);
  126.  
  127. System.out.println("datos insertados");
  128.  
  129. } catch (SQLException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. }
Add Comment
Please, Sign In to add comment