使用struts时jdbc mysql数据源连接错误

时间:2011-08-08 11:17:55

标签: java mysql database jdbc

来自Glassfish服务器日志的

收到错误

警告:RAR8054:为池[hrms / connectionPool]创建非池化[test]连接时出现异常,null 警告:RAR8054:为池[hrms / connectionPool]创建非池化[test]连接时出现异常,null

使用jsp和struts框架实现jdbc mysql数据源和连接池时出现问题。请帮忙。

代码显示.....

/*

     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package database;

    /**
     *
     * @author LenasalonM01
     */
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    import javax.swing.JOptionPane;

    public class database {

        private Connection connect;
        private Statement statement;
        private ResultSet resultset;
        private InitialContext context;
        private DataSource datasource;


        public void connection() {
            try {
    //            load driver
                  Class.forName("com.mysql.jdbc.Driver").newInstance();


    //        initialize context
                context = new InitialContext();
    //        datasource path
                datasource = (DataSource) context.lookup("jdbc/hrms");
    //        connect to datasource
                connect = datasource.getConnection();
    //         create statement from connection made
                statement = connect.createStatement();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }

        }

        public void disconnect() {
            try {
                if (resultset != null) {
                    resultset.close();
                }
                if (connect != null) {
                    connect.close();
                }
                if (context != null) {
                    context.close();
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }

        public boolean checkValid(String query) {
            try {
                connection();
                resultset = statement.executeQuery(query);
                if (resultset.next()) {
                    return true;
                } else {
                    return false;
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
                return false;
            }
        }

        public ResultSet fetchdata(String query) {
            try {
                connection();
                resultset = statement.executeQuery(query);
                disconnect();
                return resultset;

            } catch (Exception e) {
                return resultset;

            }
    //        return resultset;
        }
    }

2 个答案:

答案 0 :(得分:1)

MySQL守护程序是否正常运行?您可以使用命令行客户端连接到服务器吗?如果没有,请启动服务器并重新运行您的应用程序。

我建议你将数据库代码与用户界面内容分开。你会后悔以某种方式将两者结合起来。

答案 1 :(得分:0)

检查包含jdbc驱动程序的jar是否是服务器的库文件夹