无法使用MyEclipse中的struts与Derby数据库连接

时间:2012-03-19 11:36:49

标签: java-ee derby struts1

我是 struts MyEclipse IDE 的新手。
我开发了一个登录应用程序,它使用用户名密码并尝试将其存储在MyEclipse默认数据库中,即 Derby 。 但是它很难与Derby DB连接,我无法存储登录详细信息 我将derby jar文件放在我的项目lib文件夹中,并在APP文件夹表下的Derby数据库中创建了一个“登录”表

请帮帮我。

LoginFormBean.java

package info.strutspkg;

import javelin.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginFormBean extends ActionForm {

 private String username = null;
 private String password = null;

 public ActionErrors validate(ActionMapping mapping,
         HttpServletRequest request) {

     ActionErrors actionErrors = new ActionErrors();

     if(username == null || username.length() < 1) {
         actionErrors.add("userName", new ActionMessage("error.username"));
     }
     try {
     if(password == null || password.length()<1) {
         actionErrors.add("password", new ActionMessage("error.password"));
     }
     }catch(Exception e) {
         e.printStackTrace();
     }
     return actionErrors ;
 }

 public void setUsername(String username)
 {
  this.username = username;
 }
 public String getUsername()
 {
  return username;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 public String getPassword()
 {
  return password;
 }
}

LoginActionClass.java

package info.strutspkg;


import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import login.logindao.Logindao;

import org.apache.struts.action.*;

public class LoginActionClass extends Action{

     public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
     {
         Connection con=null;
            String key=null;
    try{

      LoginFormBean lf = (LoginFormBean)form;
      String uname = lf.getUsername();
      String pwd = lf.getPassword();


      Logindao dao=new Logindao();

       int i=dao.insert(uname,pwd);

       if(i>0){
        key="success";
       }
       else
       {
        key="failure";
       }
      }
      catch(Exception e){
       e.printStackTrace();

       }
    return mapping.findForward(key);
    }
    }

Logindao.java

package login.logindao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import db.dbconnection.DBConnect;

public class Logindao {

    public int insert(String uname,String pwd){
          int r=0;
          PreparedStatement ps = null;
          //get connection object from DBConnection class
          Connection conn = DBConnect.createConnection();
          System.out.println("Connection"+conn);

          try{
           //use PreparedStatement to insert the values of username and password
           ps=conn.prepareStatement("insert into login(username,password) values(?,?)");
           ps.setString(1, uname);
           ps.setString(2, pwd);
           r = ps.executeUpdate();
          }
          catch(SQLException ex) {
           try {
            ps.close();
            conn.close();
           } catch (SQLException e) {
            e.printStackTrace();
           }
           System.err.println("SQLException: " + ex.getMessage());
          }
          return r;

         }

}

DBConnect.java

package db.dbconnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DBConnect {

      public static Connection createConnection(){

     Connection conn = null;
     String url = "jdbc:derby://localhost:1527/myeclipse/derby;create=true";
     String userName="lalitha";
     String password="lalitha";
     String tableName = "login";


         try
            {
                Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                //Get a connection
                conn = DriverManager.getConnection(url,userName,password); 
            }
            catch (Exception except)
            {
                except.printStackTrace();
            }
            return conn;
      }
        }

堆栈跟踪:

**Connectionorg.apache.derby.client.net.NetConnection40@161401f
java.lang.NullPointerException
    at login.logindao.Logindao.insert(Logindao.java:27)
    at info.strutspkg.LoginActionClass.execute(LoginActionClass.java:28)
    at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
    at java.lang.Thread.run(Thread.java:662)
WARN  [ActionMapping] Unable to find 'null' forward.

1 个答案:

答案 0 :(得分:0)

你得到NullpointerException,因为函数createConnection()在捕获Exception创建数据库时将返回NULL:

    Connection conn = null;
     try
        {
          ...
        }
        catch (Exception except)
        {
            except.printStackTrace();
        }
        //conn is null
        return conn;

except.printStackTrace();的输出将帮助您找到该特定错误。

我想它必须对你的数据库设置做些什么,但你没有提供足够的信息来支持这个理论。