Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BosqueJDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ClasePrincipal {
- public static void main(String[] args) {
- }
- public void CrearDDBB() {
- // driver jdbc
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Credenciales
- final String CREATE_DATABASE = "CREATE DATABASE bosque";
- final String CREATE_USER = "root";
- final String CREATE_PASS = "_HidrA3773_";
- final String URL_DB = "jdbc:mysql://localhost:3306";
- Connection con = null;
- Statement stmt = null;
- // Java JDBC driver nombre y url bbdd
- try {
- System.out.println("Creando la bd...");
- con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
- stmt = con.createStatement();
- stmt.executeUpdate(CREATE_DATABASE);
- System.out.println("Base de datos creada");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void CrearTablas() {
- // driver jdbc
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Credenciales
- String CREATE_TABLE_PLANTA = "CREATE TABLE planta";
- String CREATE_TABLE_ARBUSTO = "CREATE TABLE arbusto";
- String CREATE_TABLE_ARBOL = "CREATE TABLE arbol";
- final String CREATE_USER = "root";
- final String CREATE_PASS = "_HidrA3773_";
- final String URL_DB = "jdbc:mysql://localhost:3306/bosque";
- Connection con = null;
- Statement stmt = null;
- CREATE_TABLE_PLANTA = "CREATE TABLE planta" + "(id INTEGER not NULL AUTO_INCREMENT, " + " nombre VARCHAR(50), "
- + " PRIMARY KEY ( id ))";
- CREATE_TABLE_ARBUSTO = "CREATE TABLE arbusto " + "(id INTEGER not NULL AUTO_INCREMENT, "
- + " flores VARCHAR(50), " + " PRIMARY KEY ( id ))";
- CREATE_TABLE_ARBOL = "CREATE TABLE arbol " + "(id INTEGER not NULL AUTO_INCREMENT, " + " height FLOAT, "
- + " circunference FLOAT, " + " foliageDensity INTEGER, " + " treeTrunkColor VARCHAR(50), "
- + " PRIMARY KEY ( id ))";
- ;
- // Java JDBC driver nombre y url bbdd
- try {
- System.out.println("Creando tablas...");
- con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
- stmt = con.createStatement();
- stmt.executeUpdate(CREATE_TABLE_PLANTA);
- stmt.executeUpdate(CREATE_TABLE_ARBUSTO);
- stmt.executeUpdate(CREATE_TABLE_ARBOL);
- System.out.println("Tablas creadas");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void InsertarTablas() {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- final String CREATE_USER = "root";
- final String CREATE_PASS = "_HidrA3773_";
- final String URL_DB = "jdbc:mysql://localhost:3306/bosque";
- Connection con = null;
- Statement stmt = null;
- String INSERT_INTO_PLANTAS = "INSERT INTO planta (nombre) " +
- "VALUES ('Plantas arbustivas')";
- String INSERT_INTO_ARBUSTO = "INSERT INTO arbusto (flores)" +
- "VALUES ('Buddleja alternifolia')";
- String INSERT_INTO_ARBOL = "INSERT INTO arbol (height, circunference, foliageDensity,treeTrunkColor)" +
- "VALUES (34.5,20.6,15,'Marron')";
- try {
- System.out.println("Insertando datos...");
- con = DriverManager.getConnection(URL_DB, CREATE_USER, CREATE_PASS);
- stmt = con.createStatement();
- stmt.executeUpdate(INSERT_INTO_PLANTAS);
- stmt.executeUpdate(INSERT_INTO_ARBUSTO);
- stmt.executeUpdate(INSERT_INTO_ARBOL);
- System.out.println("datos insertados");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment