从java在mysql中创建数据库

你能帮忙解决这个问题吗?

我正在尝试创建然后使用名为TIGER的数据库。

如果我在MySQL中创建数据库并且运行完美,我没有问题。

我想做的是从Java创建它。 因此,当第一次运行代码时,它会在初始启动时创建数据库。 如果可能的话,我想用干净的方法把它装箱。

有人可以告诉我你实际定位代码的位置

这是代码

private String jdbcDriver = "com.mysql.jdbc.Driver"; private String dbAddress = "jdbc:mysql://localhost:3306/"; private String dbName = "TIGER"; private String userName = "root"; private String password = ""; private PreparedStatement statement; private ResultSet result; private Connection con; public DbStuff() { try { Class.forName(jdbcDriver); con = DriverManager.getConnection(dbAddress + dbName, userName, password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

这是创建数据库的代码

  con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); statement = Conn.createStatement(); int myResult = statement.executeUpdate("CREATE DATABASE TIGER"); 

在此先感谢,任何和所有的帮助表示赞赏

对于这些错误感到抱歉,因为我是一位长期读者,但却是一位新作家。

当我尝试运行代码的主要部分时,它会生成一个SQLException,因为数据库不存在。 此时我想捕获exception并在此时创建数据库。 但是当我尝试这个时,它不会创建数据库。

试试这个代码。

 public class DbStuff { private static String jdbcDriver = "com.mysql.jdbc.Driver"; private static String dbName = "TIGER"; public static void main(String[] args) throws Exception { Class.forName(jdbcDriver); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password="); Statement s = conn.createStatement(); int Result = s.executeUpdate("CREATE DATABASE "+dbName); } } 

你总是可以跑

 int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER;") 

如果在项目启动时运行它,它将只检查数据库是否存在,如果不存在,它将创建新数据库。

供参考: http : //dev.mysql.com/doc/refman/5.0/en/create-database.html

我建议使用这段代码

 private String jdbcDriver = "com.mysql.jdbc.Driver"; private String dbAddress = "jdbc:mysql://localhost:3306/TIGER?createDatabaseIfNotExist=true"; private String userName = "root"; private String password = ""; private PreparedStatement statement; private ResultSet result; private Connection con; public DbStuff() { try { Class.forName(jdbcDriver); con = DriverManager.getConnection(dbAddress, userName, password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

解决问题的简单方法是:

 package Database; import java.sql.*; public class Database { public static void main(String[] args) { final String URl = "JDBC:mysql:/localhost:3306/info"; final String id = "root"; final String password = ""; try { Connection con1 = DriverManager.getConnection(URl,id,password); Statement s1 = con1.createStatement(); String s = "CREATE DATABASE CLASSIFIED"; s1.executeUpdate(s); } catch(SQLException err) { System.out.println(err.getMessage()); } } } 

关于一个无关(问题)的说明:

我不认为以编程方式生成数据库是个好主意。 最好使用数据库附带的实用工具。 与MySQL一起工作的每个人几乎肯定都熟悉实用工具,不必熟悉您的Java代码,更改它,编译它,并运行它来进行更改。 此外,该实用程序具有Java代码可能没有的丰富function,例如分配权限,索引,设置外键等。

首先,

我要感谢所有回答它的人确实得到不同的意见和看法。

这是我在一起的解决方案。

  public class DbStuff { private String jdbcDriver = "com.mysql.jdbc.Driver"; private String dbAddress = "jdbc:mysql://localhost:3306/"; private String userPass = "?user=root&password="; private String dbName = "TIGER"; private String userName = "root"; private String password = ""; private PreparedStatement statement; private ResultSet result; private Connection con; public DbStuff() { try { Class.forName(jdbcDriver); con = DriverManager.getConnection(dbAddress + dbName, userName, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { createDatabase(); } } private void createDatabase() { try { Class.forName(jdbcDriver); con = DriverManager.getConnection(dbAddress + userPass); Statement s = con.createStatement(); int myResult = s.executeUpdate("CREATE DATABASE " + dbName); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

随意评论任何代码。 我知道使用e.printStackTrace()不是最好的前进方式,不用担心以后会修改它。

几点需要注意和纠正 –

  1. 您已将Connection Object声明为private Connection con; 但你使用它作为statement = Conn.createStatement();
  2. 您已声明对Prepared Statement private PreparedStatement statement;的引用private PreparedStatement statement; 但是你正在创建Statement statement = Conn.createStatement(); 。 你的代码应该是 –

     try { Connection con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); Statement statement = con.createStatement(); int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER"); //should get 0 } catch (SQLException e) { System.out.println("Database creation failed"); e.printStackTrace(); } 

请注意,当executeUpdate的返回值为0时,它可能意味着以下两种情况之一:

  • 执行的语句是一个影响零行的更新语句。

  • 执行的语句是DDL语句。

文档