简体   繁体   English

如何使用Java Bean和jsp标签在Postgres数据库中插入数据?

[英]How to insert data in Postgres database using Java Beans and jsp tags?

I need your help. 我需要你的帮助。
I have to develop a Web Application which consists in a JSP and a Java Bean. 我必须开发一个包含JSP和Java Bean的Web应用程序。 The JSP file has to get two parameters (name and account) and then insert them in a database only using bean and jsp tags. JSP文件必须获取两个参数(名称和帐户),然后仅使用bean和jsp标记将它们插入数据库中。 I started to do something: 我开始做一些事情:
Java Bean: Java Bean:

    package beans;
    public class java1 {
        private String name = "";
        private String account = "";

        public String getName(){
            return name;
        }

        public String getAccount(){
            return account;
        }

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

        public void setAccount(String account){
            this.account = account;
        }
    }

JSP JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <%@page import = "java.sql.*"%>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%String nome = (String) request.getParameter("nome");
        String account = (String) request.getParameter("account");%>
        <jsp:useBean id = "esame" class = "beans.java1" scope = "page"/>
            <jsp:setProperty name = "esame" property = "name" value = "<%=nome%>"/>
            <jsp:setProperty name = "esame" property = "account" value = "<%=account%>"/>
    </head>
    <body>
        <h1>Nome: </h1>
        <jsp:getProperty name = "esame" property = "name"/>
        <h1>Account:</h1>
        <jsp:getProperty name = "esame" property = "account"/>

    </body>
</html>

Now the Java Bean and jsp page itself work good. 现在,Java Bean和jsp页面本身运行良好。 I need to know how to get those two parameters and insert them in database. 我需要知道如何获取这两个参数并将其插入数据库中。 Table structure would be: 表结构为:

users(name, account);

I know how to insert data into a database using JDBC driver, here's an example method: 我知道如何使用JDBC驱动程序将数据插入数据库,这是一个示例方法:

public static void insert(String name, String account){
        String sql = "insert into users values('" + name + "', '" + account + "');";
        String url = "jdbc:postgresql://localhost/tests";
        Connection conn = null;
        Statement st = null;
        try{
            conn = DriverManager.getConnection(url);
            st = conn.createStatement();
            st.executeUpdate(sql);
            st.close();
            conn.close();
        }
        catch(SQLException ecc){
            System.out.println(ecc.getMessage());
        }
    }

But how can I do it using ONLY java bean and jsp tags? 但是,如何仅使用Java bean和jsp标签呢? Sorry for the long question, I only wanted to be clear. 很抱歉,我的问题很长,我只想清楚一点。 Thank you 谢谢

If you ONLY want beans and jsp's (which is really bad) then you can just put the insert method in the java1 class and then call it from the jsp like 如果只需要bean和jsp(确实很糟糕),则可以将insert方法放在java1类中,然后从jsp调用它,例如

<%    esame.insert(); %>

Of course you will need a second jsp with a form for inserting the data. 当然,您将需要第二个带有表单的jsp,用于插入数据。 If the user submits then you call a third jsp that calls the insert method. 如果用户提交,则您调用第三个jsp,后者调用insert方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM