我有一个MySQL脚本,当我将其连接时都可以使用,但是它给出一个错误,提示“ testDB.users”不存在。由此得出的结论是,我没有将MySQL脚本连接到该类。如何正确完成?这是我在课堂上获得联系的地方。

private static DBManager instance;

    public static synchronized DBManager getInstance() throws DBException {
        if (instance == null) {
            instance = new DBManager();
        }
        return instance;
    }

    private DBManager() throws DBException {
        try {
            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            // ST4DB - the name of data source
            ds = (DataSource) envContext.lookup("jdbc/testDB_MySQL");
            LOG.trace("Data source ==> " + ds);
        } catch (NamingException ex) {
            ex.printStackTrace();
            LOG.error(Messages.ERR_CANNOT_OBTAIN_DATA_SOURCE, ex);
            throw new DBException(Messages.ERR_CANNOT_OBTAIN_DATA_SOURCE, ex);
        }
    }

    private DataSource ds;
public Connection getConnection() throws DBException {
        Connection con = null;
        try {
            con = ds.getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
            LOG.error(Messages.ERR_CANNOT_OBTAIN_CONNECTION, ex);
            throw new DBException(Messages.ERR_CANNOT_OBTAIN_CONNECTION, ex);
        }
        return con;
    }

public User findUserByLogin(String login) throws DBException {
        User user = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection con = null;
        try {
            con = getConnection();
            pstmt = con.prepareStatement(SQL_FIND_USER_BY_LOGIN);
            pstmt.setString(1, login);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                user = extractUser(rs);
            }
            con.commit();
        } catch (SQLException ex) {
            rollback(con);
            ex.printStackTrace();
            throw new DBException(Messages.ERR_CANNOT_OBTAIN_USER_BY_LOGIN, ex);
        } finally {
            close(con, pstmt, rs);
        }
        return user;
    }


MySQL脚本:

CREATE TABLE roles(

        id INTEGER NOT NULL PRIMARY KEY,
        name VARCHAR(10) NOT NULL UNIQUE
    );
    INSERT INTO roles VALUES(0, 'admin');
    INSERT INTO roles VALUES(1, 'client');

    CREATE TABLE users(
        id INTEGER NOT NULL generated always AS identity PRIMARY KEY,
        login VARCHAR(10) NOT NULL UNIQUE,
        password VARCHAR(10) NOT NULL,
        first_name VARCHAR(20) NOT NULL,
        last_name VARCHAR(20) NOT NULL,

            ON DELETE CASCADE
            ON UPDATE RESTRICT
    );

    INSERT INTO users VALUES(DEFAULT, 'admin', 'admin', 'Harry', 'Potter', 0);
    INSERT INTO users VALUES(DEFAULT, 'client', 'client', 'Peter', 'Parker', 1);


context.xml属性文件

java - 如何在Java中运行MySQL脚本?-LMLPHP

我需要使用数据源。
如何在课堂上运行MySQL脚本?或其他什么方法呢?

最佳答案

是否可以为此使用配置文件,如在Derby DB中使用的?

ant-derby.xml

<project name="Derby">
<!--
 Derby home (we use the internal JavaDB of JDK (= Apache Derby)
-->
    <property name="derby.home" value="C:\Program Files\Java\jdk1.7.0_67\db" />
    <!--  Derby libraries  -->
    <path id="derby.classpath">
        <fileset dir="${derby.home}/lib">
            <include name="*.jar" />
        </fileset>
    </path>
    <!--  To start/stop derby server script (macros)  -->
    <macrodef name="server">
        <attribute name="arg" />
        <sequential>
            <java classname="org.apache.derby.drda.NetworkServerControl">
                <arg value="@{arg}" />
                <classpath refid="derby.classpath" />
            </java>
        </sequential>
    </macrodef>
    <!--  To start the Derby server  -->
    <target name="server-start">
        <server arg="start" />
    </target>
    <!--  To stop the Derby server  -->
    <target name="server-stop">
        <server arg="shutdown" />
    </target>
    <!--  To run script (macros)  -->
    <macrodef name="run">
        <attribute name="script" />
        <sequential>
            <java classname="org.apache.derby.tools.ij" fork="on">
                <arg value="@{script}" />
                <classpath refid="derby.classpath" />
            </java>
        </sequential>
    </macrodef>
    <!--  To run db-create.sql script  -->
    <target name="run-testing">
        <run script="sql/Testing.sql" />
    </target>
    <!--  To select run select.sql script  -->
    <target name="run-select">
        <run script="sql/select.sql" />
    </target>
</project>


这应该可以帮助我控制连接池。还有其他方法吗?

07-28 01:39