我有一个注册。jag(jaggery页面)供用户注册。我有一个注册.js(javascript)用于javascript验证。
我想通过java(即jdbc)将用户信息存储在mysql数据库中,我已经为jdbc编写了以下代码,文件名为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。
我终于解决了!
我创建了一个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()
}
%>
它真的很好用