使用Struts时jdbc mysql数据源连接错误 [英] jdbc mysql datasource connection error while working with struts

查看:111
本文介绍了使用Struts时jdbc mysql数据源连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Glassfish服务器日志中出现以下错误

from the Glassfish server log am getting an error below

警告:RAR8054:为池[hrms/connectionPool]创建未池化的[test]连接时发生异常,为null 警告:RAR8054:为池[hrms/connectionPool]创建未池化的[test]连接时发生异常,null

WARNING: RAR8054: Exception while creating an unpooled [test] connection for pool [ hrms/connectionPool ], null WARNING: RAR8054: Exception while creating an unpooled [test] connection for pool [ hrms/connectionPool ], null

在使用jsp和struts框架实现jdbc mysql数据源和连接池时遇到问题.请帮忙.

am getting a problem in implementing jdbc mysql datasource and connection pooling using jsp and struts framework. please help.

显示代码....

/*

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

推荐答案

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

Is the MySQL daemon up and running? Can you connect to the server using the command line client? If not, start the server and re-run your application again.

我建议您将数据库代码与用户界面内容分开.您会后悔有一天将两者结合在一起.

I would advise you to separate your database code from user interface stuff. You'll regret combining the two the way you have someday.

这篇关于使用Struts时jdbc mysql数据源连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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