0

我有一个signup.jag (粗略的页面)供用户注册。我有一个用于 javascript 验证的signup.js (javascript) 。

我想通过 java 即 jdbc 将用户信息存储在 mysql 数据库中,为此我编写了以下文件名sigunp.java的代码:

import java.io.*;
import java.lang.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;

public class signupdb extends HttpServlet
{
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException IOException
    {
            response.setContentType(text/html);
            PrintWriter pw = response.getWriter();

            String connectionURL = "jdbc:mysql://localhost:3306/ticketing";
            Connection connection;
            try
            {
                    String firstname = request.getParameter("fname");
                    String lastname = request.getParameter("lname");
                    String phonenum = request.getParameter("phone");
                    String addr = request.getParameter("address");
                    String email = request.getParameter("email");
                    String password = request.getParameter("pass");

                    pw.println(firstname);
                    pw.println(lastname);
                    pw.println(phonenum);
                    pw.println(addr);
                    pw.println(email);
                    pw.println(password);

                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection(connectionurl,"root","suselinux");
                    PreparedStatement pst = connection.prepareStatement("insert into userinfo values(?,?,?,?,?,?)");
                    pst.setString(1,firstname);
                    pst.setString(2,lastname);
                    pst.setString(3,phonenum);
                    pst.setString(4,addr);
                    pst.setString(5,email);
                    pst.setString(6,password);

                    int i = pst.executeUpdate();

                    if(i != 0)
                    {
                            pw.println("<br>Record has been inserted");
                    }
                    else
                    {
                            pw.println("No Data Inserted");
                    }
            }
            catch(Exception e)
            {
                    pw.println(e);
            }
    }     
}

但是当我启动jaggery 服务器并提交数据时,它显示错误 404

4

1 回答 1

0

最后我解决了!

我创建了一个con.jag文件,其中包含名为users的

<%

var query1 = "create table users (id int not null auto_increment, primary key(id), first_name varchar(15), last_name varchar(15), phone varchar(20), address varchar(50), email varchar(30), pass varchar(30));";


config();

var db = new Database ("jdbc:mysql://localhost:3306/tickets", "root", "password", config);

try
{
        db.query(query1);
        print("Table Created again");
} catch (e)
        {
                print(e.toString());
        }
        finally
                {
                        db.close()
                }
%>

然后使用insert_data .jag 文件将数据插入数据库:

<%

var frst_name = request.getParameter('fname');
var lst_name = request.getParameter('lname');
var country_name = request.getParameter('country_code');
var phon = request.getParameter('phone');
var addr = request.getParameter('address');
var mail = request.getParameter('email');
var pwd = request.getParameter('pass');

var data = "insert into users(first_name, last_name,country, phone, address, email, pass) values ('"+frst_name+"','"+lst_name+"','"+country_name+"', '"+phon+"','"+addr+"','"+mail+"','"+pwd+"')";



config();
var db = new Database ("jdbc:mysql://localhost:3306/tickets", "root", "password", config);

try
{
        db.query(data);
} catch(e)
        {
                print(e.toString());
        } finally
                {
                        db.close()
                }
%>

它真的很好用!

于 2014-11-26T11:26:31.753 回答