在mysql中从java创建数据库 [英] creating a database in mysql from java

查看:155
本文介绍了在mysql中从java创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以帮助解决这个问题。



我正在尝试创建一个名为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语句;
private ResultSet result;
private Connection con;

public DbStuff(){
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + dbName,userName,password);
}

catch(ClassNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
}
catch(SQLException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

这里是创建数据库的代码



  con = DriverManager.getConnection(jdbc:mysql:// localhost /?user = root& password = rootpassword); 
statement = Conn.createStatement();
int myResult = statement.executeUpdate(CREATE DATABASE TIGER);感谢提前感谢任何和所有的帮助









$ b

对于错误,我是一个长期的读者,但一个新的作家。



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

解决方案

尝试此代码。

  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 =);
语句s = conn.createStatement();
int Result = s.executeUpdate(CREATE DATABASE+ dbName);
}
}


Could you help with this problem.

I'm trying to create and then use a database called TIGER.

I have no problem if I create the database in MySQL and it runs perfectly.

What I would like to do is create it from Java. So that when the code is being run for the first time it creates the database as part of the initial launch. I would like to box it up in a nice clean method if possible.

Could it be possible for someone to show me where you actually position the code

Here is the code

    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();
        }
    }

and here is the code to create the database

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

Thanks in advance and any and all help is appreciated

Sorry for the mistakes as I'm a long term reader but a new writer.

When I try do run the main part of the code it generates an SQLException because the database doesn't exist. At this point I would like to catch the exception and create the database at that point. But when I try this it doesn't create the database.

解决方案

Try with this code.

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);
    }
}

这篇关于在mysql中从java创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆