抛出空指针异常

时间:2014-02-05 12:16:11

标签: struts2

在我的Struts应用程序中,Jsp中有一个Register表单。然后,通过动作类将输入的值存储到mysql Db中。

用于向mysql Db插入值的动作类(Register.java)是:

 package com.login;

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

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {



String regname;
String regpass;
String regmail;

public String getRegname() {
    return regname;
}

public void setRegname(String regname) {
    this.regname = regname;
}

public String getRegpass() {
    return regpass;
}

public void setRegpass(String regpass) {
    this.regpass = regpass;
}

public String getRegmail() {
    return regmail;
}

public void setRegmail(String regmail) {
    this.regmail = regmail;
}


Connection con;
 Statement st;
 ResultSet rs;

public void connect(){

    try {

        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vijay","root", "pwd");

        //*.getConnection("jdbc:mysql://localhost:3307/shoppingmall","root", "vijay");      
        st=con.createStatement();*

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {          
        e.printStackTrace();
    }
}

public void queree(){
    try {
        //*st.executeUpdate("insert into prodet (name,password,email) values('"+this.getRegname()+"','"+this.getRegpass()+"','"+this.getRegmail()+"');");*

        st.executeUpdate("insert into prodet (name,caty) values('"+this.getRegname()+"','"+this.getRegpass()+"');");
    } catch (SQLException e) {

        e.printStackTrace();
    }
}


@Override
public String execute() throws Exception {
    this.connect();
    this.queree();

    return SUCCESS;
}
}

Jsp:

<s:form action="/loginns/register" cssStyle="float:right; background-color:lightgreen" method="POST">
<h5 align="center">Register here</h5>   <br>
<s:textfield name="regname" label="UserName"></s:textfield>
<s:textfield name="regpass" label="Password"></s:textfield>
<s:textfield name="regmail" label="email"></s:textfield>
<s:submit align="center" value="Register"></s:submit>
</s:form>

struts.xml中:

<package name="Login" namespace="/loginns" extends="struts-default">
<action name="register" class="com.login.Register">
        <result name="error">/index.jsp</result>
        <result name="success">/Registered.jsp</result>
    </action>

</package>

一切正常。单击“注册”按钮后,它会成功显示结果页面。没有异常被抛出。 但是在Db上,值不会更新。试过2个Dbs但结果相同。 (斜体代码用于Mysql workbench 6.0中的Db)我错过了什么?

1 个答案:

答案 0 :(得分:0)

您必须为更新和插入调用executeUpdate,仅为选择调用executeQuery。

http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html