使用java jdbc和prepared语句的CREATE DATABASE查询返回语法错误

时间:2015-08-01 15:00:17

标签: java mysql jdbc prepared-statement

我正在尝试让Java使用JDBC创建数据库但是我得到了语法错误,尽管查询是正确的。例如,如果我明确地将数据库的名称写入代码中,它就可以正常工作。这是我的代码:

package mysql_manipulator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Create {
    private static final String 
            driver = "com.mysql.jdbc.Driver",
            url = "jdbc:mysql://localhost",
            username = "dylan",
            password = "******";
    private String databaseName;

    public Create (){
        this.databaseName = null;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public void createDatabase(String databaseName){
        try {
            this.setDatabaseName(databaseName);        
            Class.forName(driver);
            Connection con = DriverManager.getConnection(url,username, 
                password);
            String query = "CREATE DATABASE ?";
            PreparedStatement preparedStmt = con.prepareStatement(query);
            preparedStmt.setString (1,this.getDatabaseName());
            preparedStmt.execute();
            preparedStmt.close();
            con.close();        
        }
        catch (Exception anException){
            System.out.println("Error: " + anException);
        }
    }
}  

我的主要课程

package mysql_manipulator;

public class Mysql_manipulator {
    public static void main(String[] args) {
        Create myObj = new Create();
        myObj.createDatabase("whatevs");        
    }    
}

错误

Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You  have an error in
your SQL syntax; check the manual that corresponds to your  MariaDB server version 
for the right syntax to use near ''whatevs'' at line 1

非常感谢任何知道为什么这不起作用的人 - 我在网上搜索都无济于事。

2 个答案:

答案 0 :(得分:5)

您不能将数据库名称作为绑定变量 - 您必须将其嵌入查询文字中。请将您的查询更改为:

/**
 * Maps to parent table in database
 */
@Entity
class Parent {
    @Id
    long id;

    @OneToMany(mappedBy="parent")
    Set<Relationship> relationships;
}

/**
 * Maps to student table in database
 */
@Entity
class Student {
    @Id
    long id;

    @OneToMany(mappedBy="student")
    Set<Relationship> relationships;
}

/**
 * Encapsulates primary key in relationship table
 */
class RelationshipId {
    long studentId;
    long parentId;

    @Override
    public int hashCode() {
        return ((Long)(studentId + parentId)).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null
                && obj instanceof RelationshipId
                && ((RelationshipId)obj).studentId == studentId
                && ((RelationshipId)obj).parentId == parentId;
    }
}

/**
 * Maps to relationship table in database
 */
@Entity
@IdClass(RelationshipId.class)
class Relationship {
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name="parent")
    Parent parent;

    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name="student")
    Student student;

    @Column
    String childRelationship;
}

您还可以使用普通String query = "CREATE DATABASE " + getDatabaseName(); (而不是Statement)来运行此SQL。

答案 1 :(得分:0)