如何设置Hibernate配置属性以使用Fabric驱动程序连接到Mysql

时间:2015-04-23 17:27:39

标签: java mysql sql hibernate mysql-fabric

我正在尝试使用一些旧的代码来为Mysql数据库中的hibernate配置创建一些表,但代码引用了我不再拥有的数据源配置,我只想将其作为独立Java运行(不在Tomcat容器内)所以我对Hibernate配置进行了一些更改。

我的pom现在包括

  public static  Configuration setupDatabase()
{
    Configuration cfg = new Configuration()
            .setProperty(Environment.URL,"jdbc:mysql:songkong")
            .setProperty(Environment.DRIVER,"com.mysql.fabric.jdbc.FabricMySQLDriver")
            .setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect")
            .setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.username","dbuser")
            .setProperty("hibernate.connection.username", "dbpassword");;
    addEntitiesToConfig(cfg);
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    return cfg;
}

public static void main(String[] args)
{
    Configuration cfg = setupDatabase();
    new SchemaExport(cfg).create(false,true);
}

我的代码是

java.sql.SQLException: No suitable driver found for jdbc:mysql:songkong
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:154)
        at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
        at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:55)
        at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
        at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:368)
        at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305)
        at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294)
        at com.jthink.songkong.appserverdb.util.Db.main(Db.java:143)

目前问题是它无法找到驱动程序。

Sub HideOther(Color_Index)
    Dim cells As Range

    Set rRange = Range("$A4:$A313")
    For Each cl In rRange
        currentColIndex = cl.Interior.ColorIndex
        If currentColIndex <> Color_Index Then
            If Not cells Is Nothing Then
                Set cells = Union(cells, cl)
            Else
                Set cells = cl
            End If
        End If
    Next cl

    cells.EntireRow.Hidden = True
End Sub

Fabric驱动程序在jar中,但我不确定是否应该使用此驱动程序,并且如果Environment.DRIVER行设置正确以引用它。我认为之前(几年前)我没有使用过这款驱动程序,但它是我在maven中心找到的唯一一款。

(我的数据库名为songkong,我可以从命令行正确更正,我在代码摘录中没有显示真实的用户名和密码)

1 个答案:

答案 0 :(得分:1)

注意到非结构驱动程序也在jar中,将ENVIRONMENT.DRIVER设置为此,并对Environment.URL进行了一些修改。

 Configuration cfg = new Configuration()
                .setProperty(Environment.URL,"jdbc:mysql://localhost/songkong")
                .setProperty(Environment.DRIVER,"com.mysql.jdbc.Driver")
                .setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect")
                .setProperty("hibernate.show_sql", "true")
                .setProperty("hibernate.connection.username","dbuser")
                .setProperty("hibernate.connection.password", "dbpassword");;