私はstrutsとMyEclipse IDEを初めて使用しています。
私はユーザ名とパスワードを取り、MyEclipseのデフォルトのデータベースすなわちダービーに保管しようとするとログインアプリケーションを開発しました。
しかし、Derby DBとの接続に失敗し、ログインの詳細を保存できません。
私のプロジェクトのlibフォルダにderby jarファイルを置き、DerbyデータベースのAPPフォルダテーブルの下に「ログイン」テーブルを作成しましたMyEclipseのストラットを使用して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;
}
}
のStackTrace:
**[email protected]
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.
ありがとうございます、Lalitha – Vidya
正確にはどのようなエラーがありますか? AFAIKは、デフォルトではまだ起動されていないため、内部DBを手動で起動する必要があります。乾杯! – SimonSez
1)エラーの完全なスタックトレースを投稿してください2)derbyネットワークサーバーがオンラインで、localhost:1527で実行していますか? – oers