我有一个 signup.jag (jaggery page)供用户注册。 我有一个 signup.js (javascript)用于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 。
答案 0 :(得分:0)
最后我解决了!
我创建了一个 con.jag 文件,其 表 名为用户:
<%
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()
}
%>
它真的很好用!