Java 如何在jsp中使用bean类在数据库中插入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22225854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:26:13  来源:igfitidea点击:

How to insert value in database using bean class in jsp?

javajspjdbcjavabeans

提问by

Hi iam tring to insert value in database using bean class in jsp form and i am able to this but i am getting little bit problem whenever i insert value in table null value also insert with them i tried not i am not able to get that where i m wrong please solve my problem

嗨,我正在使用jsp形式的bean类在数据库中插入值,我能够做到这一点,但是每当我在表中插入值时都会遇到一些问题,空值也插入它们我试过没有我无法得到那个地方我错了请解决我的问题

my bean.java

我的 bean.java

package com.javabean;


import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class bean 
{

    private String msgid;
    private String message;
    private Connection connection=null;
    private ResultSet rs = null;
    private Statement st = null;
    String connectionURL = "jdbc:mysql://localhost:3306/JspBean";


    public bean() 
    {
         try {
             // Load the database driver
            Class.forName("com.mysql.jdbc.Driver");
            // Get a Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            }catch(Exception e){
            System.out.println("Exception is ;"+e);
            }

    }  
    public void setmsgid(String msgid)
    {
        this.msgid = msgid;
    }

    public String getmsgid()
    {
        return (this.msgid);
    }

    public void setmessage(String message)
    {
        this.message = message;
    }

    public String getmessage()
    {
        return (this.message);
    }

    public void insert()
    {

       try
       {
            String sql = "insert into login(messageid,message) values('"+msgid+"','"+message+"')";
            Statement s = connection.createStatement();
            s.executeUpdate (sql);
            s.close ();
        }catch(Exception e){
            System.out.println("Exception is ;"+e);
        }
    }

}

here is my jspbean.jsp file

这是我的 jspbean.jsp 文件

<%@ page language="Java" import="java.sql.*" %>

<html>
    <head><title>JSP with Javabeans</title></head>
<body bgcolor="#ffccff">
<h1>JSP using JavaBeans example</h1>
    <form name="form1" method="POST">

         ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type="text" name ="msgid"> <br>
         Message<input type="text" name ="message"> <br>
         <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type = "submit" value="Submit">
         <jsp:useBean id="sample" class="com.javabean.bean" scope="page">
            <jsp:setProperty name="sample" property="*"/>
        </jsp:useBean>
    </form>
    <% sample.insert();%>
</body>
</html>

how can i achieve this please solve my problem hanks in advance T

我怎样才能做到这一点,请提前解决我的问题 T

回答by jmail

Your doing some mistake:

你做错了:

public void setmsgid(String msgid)
    {
        this.msgid = msgid;
    }
    public String getmsgid()
    {
        return (this.msgid);
    }

you should change as:

你应该改变为:

public String getmsgid()
    {
        return msgid;
    }
public void setmsgid(String msgid)
    {
        this.msgid = msgid;
    }

and add this one also:

并添加这个:

public String toString()
{
    return "User[msgid="+msgid+",message="+message+"];

}

you should must change this one:

你应该改变这个:

insert into login(username,password) values('"+msgid+"','"+message+"')";


insert into login(username,password) values(?,?)";

update:

更新:

insert into login(messageid,message) values(?,?)";