我使用java社区,也许这是个问题。因为一切都在终极版中运行。但我可以 create table 只有在工作台和社区。最有趣的是我能 drop table
create table
drop table
t2a7ltrp1#
Below is the sample code for executing DDL statements. Reference URL https://examples.javacodegeeks.com/core-java/sql/jdbc-ddl-example/ package com.jdbc.ddl.example; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class JdbcExample { // JDBC Driver Name & Database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String JDBC_DB_URL = "jdbc:mysql://localhost:3306"; // JDBC Database Credentials static final String JDBC_USER = "root"; static final String JDBC_PASS = "root"; public final static Logger logger = Logger.getLogger(JdbcExample.class); public static void main(String[] args) { Connection connObj = null; Statement stmtOBj = null; try { Class.forName(JDBC_DRIVER); connObj = DriverManager.getConnection(JDBC_DB_URL, JDBC_USER, JDBC_PASS); stmtOBj = connObj.createStatement(); // DDL Statement 1 - Create Database Schema! logger.info("\n=======CREATE DATABASE======="); stmtOBj.executeUpdate("CREATE DATABASE Demo1"); logger.info("\n=======DATABASE IS SUCCESSFULLY CREATED=======\n"); logger.info("\n=======USING DATABASE=======\n"); stmtOBj.executeUpdate("use Demo1"); // DDL Statement 2 - Create Table! logger.info("\n=======CREATE TABLE======="); stmtOBj.executeUpdate("CREATE TABLE Employee (\r\n" + " EmpId int,\r\n" + " LastName varchar(255),\r\n" + " FirstName varchar(255),\r\n" + " Address varchar(255),\r\n" + " City varchar(255)\r\n" + ");"); logger.info("\n=======TABLE IS SUCCESSFULLY CREATED=======\n"); logger.info("\n=======SHOW TABLE STRUCTURE======="); showDbTableStructure(); logger.info("\n=======DROP DATABASE======="); stmtOBj.executeUpdate("DROP DATABASE Demo1"); logger.info("\n=======DATABASE DROPPED SUCCESSFULLY=======\n"); } catch(Exception sqlException) { sqlException.printStackTrace(); } finally { try { if(stmtOBj != null) { stmtOBj.close(); // Close Statement Object } if(connObj != null) { connObj.close(); // Close Connection Object } } catch (Exception sqlException) { sqlException.printStackTrace(); } } } // This Method Is Used To Print The Table Structure private static void showDbTableStructure() throws SQLException { StringBuilder builderObj = new StringBuilder(); DatabaseMetaData metaObj = DriverManager.getConnection(JDBC_DB_URL, JDBC_USER, JDBC_PASS).getMetaData(); ResultSet resultSetObj = metaObj.getColumns("Demo1", null, "Employee", "%"); builderObj.append("Employee" + " Columns Are?= ("); while (resultSetObj.next()) { String columnName = resultSetObj.getString(4); builderObj.append(columnName).append(", "); } builderObj.deleteCharAt(builderObj.lastIndexOf(",")).deleteCharAt(builderObj.lastIndexOf(" ")).append(")").append("\n"); logger.info(builderObj.toString()); } }
1条答案
按热度按时间t2a7ltrp1#