分类: Java
2008-11-18 09:13:25
package user;//存放UserInfo.java的包,该包中放在web-inf目录下
public class UserInfo
{
private String password;
private String name;
//获取信息
public String getPassword()
{
return this.password;
}
public String getName()
{
return this.name;
}
//设置信息
public void setPassword(String p)
{
this.password=p;
}
public void setName(String n)
{
this.name=name;
}
}
package user;//同理也放在这个包中
import java.sql.*;
public class UserRegist
{
private UserInfo userInfo;//在此javabean中把UserInfo.java这个javabean引进来
private Connection conn=null;
//连接数据库
public UserRegist()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/joke?user=root");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setUserInfo(UserInfo userInfo)
{
this.userInfo=userInfo;
}
//进行注册
public void regist()throws Exception
{
String reg="insert into user_Info values(?,?)";
try
{
PreparedStatement pstmt=conn.prepareStatement(reg);
//create a preparestatement sentence ,then to set their name
pstmt.setString(1,userInfo.getPassword());
pstmt.setString(2,userInfo.getName());
//excute update
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
}