register.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
这是一个注册页面
adduser.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
将用户信息加入数据库 <%
String id = request.getParameter("id");
String name = request.getParameter("name");
String date = request.getParameter("date");
String text = request.getParameter("text");
ResultSet rs=null;
String str="select id,name,date,text from User where name='"+name+"';";
query.connect();
rs=query.select(str);
rs.last();
str="insert into User(id,name,date,text)values('"+id+"','"+name+"','"+date+"','"+text+"');";
query.update(str);
out.println("提交信息成功!");
out.print("本页面将在3秒后自动跳转到上一页面!");
out.print("
");
%>
附加数据库操作类:
// 本类用来建立与数据库的连接
package user;
import java.sql.*;
public class dbfunction {
private static Connection con = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public void connect(){
String url = "jdbc:mysql://localhost:3306/bookshop"; /在这里修改数据库名
try {
Class.forName("com.mysql.jdbc.Driver");/在这里修改数据库,我用的是Mysql
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop?user=root&password=123456&useUnicode=true&characterEncoding=GB2312");/在这里修改数据库Mysql的信息
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet select(String query)
{
System.out.println(query);
try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void update(String upstr)
{
System.out.println(upstr);
try {
stmt = con.createStatement();
int rs = stmt.executeUpdate(upstr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close()
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在我的机子上做了一遍可以,你试试吧。。。