无法在 MyEclipse 中使用 struts 连接 Derby 数据库
Posted
技术标签:
【中文标题】无法在 MyEclipse 中使用 struts 连接 Derby 数据库【英文标题】:Unable to connect with Derby database using struts in MyEclipse 【发布时间】:2012-03-19 11:36:49 【问题描述】:我是 struts 和 MyEclipse IDE 的新手。 我开发了一个登录应用程序,它采用 username 和 password 并尝试将其存储在 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.
【问题讨论】:
你到底得到了什么错误? AFAIK 您必须手动启动内部数据库,因为默认情况下它尚未启动。干杯! 1) 请发布错误的完整堆栈跟踪 2) derby 网络服务器是否在线并在 localhost:1527 下运行? 是的,我开始了德比战..这是我得到的错误.. Connectionorg.apache.derby.client.net.NetConnection40@dd20b6 java.lang.NullPointerException 【参考方案1】:您会得到 NullpointerException,因为函数 createConnection()
在捕获创建数据库的异常时将返回 NULL:
Connection conn = null;
try
...
catch (Exception except)
except.printStackTrace();
//conn is null
return conn;
except.printStackTrace();
的输出将帮助您找到特定的错误。
我认为这与您的数据库设置有关,但您没有提供足够的信息来支持该理论。
【讨论】:
以上是关于无法在 MyEclipse 中使用 struts 连接 Derby 数据库的主要内容,如果未能解决你的问题,请参考以下文章
myeclipse5.5+tomcat5.5.20+eclipse3.2+struts1+jdk1.5项目无法运行
我有一个基于struts框架的myeclipse整个工程文件,我想自己手动部署到tomcat中 有哪位大侠能指点一下
Struts1.3——使用MyEclipse集成工具开发Struts