使用java netbeans

时间:2016-05-31 06:15:39

标签: java mysql

这是一个简单的插入数据库的程序。没有错误但是插入值只有2列(年份和主题)在所有其他4列中受到影响它显示空值。我是否要更正它.................................................. .................................................. 。

文件名-index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>INSERT</title>
     <link rel="stylesheet" type="text/css" href="style1.css" />
</head>
<body>
    <h1>Insert File Details</h1>
    <form action="loginprocess.jsp">
        <table border="1">
            <tbody>
                <tr>
                    <td>File No :</td>
                    <td><input type="text" name="File No" value="" size="50"    /></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><input type="text" name="Name" value="" size="50" />  </td>
                </tr>
                <tr>
                    <td>Place of Origin :</td>
                    <td><input type="text" name="origin" value="" size="50"   /></td>
                </tr>
                <tr>
                    <td>Year :</td>
                    <td><input type="text" name="year" value="" size="50" />  </td>
                </tr>
                <tr>
                    <td>Subject :</td>
                    <td><input type="text" name="subject" value="" size="50"   /></td>
                </tr>
                <tr>
                    <td>ISBN :</td>
                    <td><input type="text" name="ISBN" value="" size="50" /> </td>
                </tr>

            </tbody>
        </table>
        <input type="reset" value="Clear" name="Clear" />
        <input type="submit" value="Submit" name="Submit" />

    </form>
 </body>
 </html>

文件名LoginDao.java

package bean;
import java.sql.*;


public  class LoginDao {
    public static boolean validate(LoginBean bean){
        boolean status=false;
        try{
            Connection conn=ConnectionProvider.getConn();
            String sql="insert into file(Fileno,name,place,year,subject,ISBN) values (?,?,?,?,?,?)";            
            PreparedStatement pstmt =conn.prepareStatement(sql);
            pstmt.setInt(1, bean.getfileno()); 
            pstmt.setString(2,bean.getname());
            pstmt.setString(3, bean.getoriginPlace());
            pstmt.setInt(4, bean.getyear());
            pstmt.setString(5, bean.getsubject());
            pstmt.setInt(6, bean.getisbn());

            int rs=pstmt.executeUpdate();
            if(rs>0){status=true;}
        }catch(Exception e){}

        return status;
    }
}

文件名LoginBean.java

package bean;                                                                          

public class LoginBean {
    private String name,subject,originPlace;
    private int fileno,year,isbn;

    public int getfileno() {
        return fileno;
    }

    public void setfileno(int fileno) {
        this.fileno = fileno;
    }

    public String getname() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    public String getoriginPlace() {
        return originPlace;
    }

    public void setoriginPlace(String originPlace) {
        this.originPlace = originPlace;
    }

    public int getyear() {
        return year;
    }

    public void setyear(int year) {
        this.year = year;
    }

    public String getsubject() {
        return subject;
    }

    public void setsubject(String subject) {
        this.subject = subject;
    }

    public int getisbn() {
        return isbn;
    }

    public void isbn(int isbn) {
        this.isbn = isbn;
    }

}

文件名-loginprocess.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
<title></title>
</head>
<body>
<%@page import="bean.LoginDao"%>
<jsp:useBean id="obj" class="bean.LoginBean"/>

<jsp:setProperty property="*" name="obj"/>

<%
 boolean status=LoginDao.validate(obj);
 if(status){
 out.println("success");
 session.setAttribute("session","TRUE");
 }
 else
 {
 out.print("failed");}

 %>
<jsp:include page="index.jsp"></jsp:include>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

如果你使用的是jsp benas标签,那么类(bean类)中的属性名称和<input >名称是jsp必须是相同的。只有两个字段具有相同的名称。所以只插入两个值。

In class       in jsp 
 File No        fileno

所以在class和bean中添加相同的名称。

答案 1 :(得分:0)

我认为Java无法将JSP文件中<input>元素中的名称与bean中正确的setter方法相匹配。例如,你有这个元素和setter:

<input type="text" name="Name" value="" size="50" />

public void setname(String name) {
    this.name = name;
}

且该属性从未设置(由于使用setname()而不是setName()),但这确实有效:

<input type="text" name="year" value="" size="50" />

public void setyear(int year) {
    this.year = year;
}

因为Java可以将year(小写)与正确的setter匹配。

尝试使用以下代码:

<tr>
    <td>File No :</td>
    <td><input type="text" name="FileNo" value="" size="50"    /></td>
</tr>
<tr>
    <td>Name</td>
    <td><input type="text" name="Name" value="" size="50" />  </td>
</tr>
<tr>
    <td>Place of Origin :</td>
    <td><input type="text" name="Origin" value="" size="50"   /></td>
</tr>
<tr>
    <td>Year :</td>
    <td><input type="text" name="Year" value="" size="50" />  </td>
</tr>
<tr>
    <td>Subject :</td>
    <td><input type="text" name="Subject" value="" size="50"   /></td>
</tr>
<tr>
    <td>ISBN :</td>
    <td><input type="text" name="ISBN" value="" size="50" /> </td>
</tr>

使用此bean定义:

public class LoginBean {
    private String name,subject,originPlace;
    private int fileno,year,isbn;

    public int getFileNo() {
        return fileno;
    }

    public void setFileFo(int fileno) {
        this.fileno = fileno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOrigin() {
        return originPlace;
    }

    public void setOrigin(String originPlace) {
        this.originPlace = originPlace;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getISBN() {
        return isbn;
    }

    public void setISBN(int isbn) {
        this.isbn = isbn;
    }
}