📄 accountdao.java
字号:
package com.webshop.db;
import java.sql.*;
import com.webshop.domain.Account;
/**
* @author w
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class AccountDAO {
Connection con;//数据库连接
String newAccount="insert into account values(?,?,?,?,?,?,?,?,?,?,?,?)";
String signon="select * from account where userId=? and password=?";
String selectAccountById="select * from account where userId=?";
/**
* 构造方法
*/
public AccountDAO()
{
}
/**
* 增加新的帐号
* @param Account
* @return
* table_Info:
* userid varchar(80) not null,
password varchar(25) not null,
email varchar(80) not null,
name varchar(80) not null,
status varchar(2),
addr1 varchar(80) not null,
addr2 varchar(40),
city varchar(80) not null,
state varchar(80) not null,
zip varchar(20) not null,
country varchar(20) not null,
phone varchar(80) not null,
*/
public void createNewAccount(Account account)throws Exception
{
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw e;
}
try
{
PreparedStatement pstmt=con.prepareStatement(newAccount);
pstmt.setString(1,account.getUserId());
pstmt.setString(2,account.getPassword());
pstmt.setString(3,account.getEmail());
pstmt.setString(4,account.getName());
pstmt.setString(5,"0");
pstmt.setString(6,account.getAddr1());
pstmt.setString(7,account.getAddr2());
pstmt.setString(8,account.getCity());
pstmt.setString(9,account.getState());
pstmt.setString(10,account.getZip());
pstmt.setString(11,account.getCountry());
pstmt.setString(12,account.getPhone());
pstmt.executeUpdate();
con.close();
}
catch(SQLException e)
{
throw e;
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
}
/**
* 根据用户的ID获得帐号信息
*/
public Account getAccountById(String userId)throws Exception
{
Account account=null;
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw e;
}
try
{
PreparedStatement pstmt=con.prepareStatement(selectAccountById);
pstmt.setString(1,userId);
ResultSet rst=pstmt.executeQuery();
if(rst.next())
{
account=new Account();
account.setUserId(userId);
account.setAddr1(rst.getString("addr1"));
account.setAddr2(rst.getString("addr2"));
account.setCity(rst.getString("city"));
account.setState(rst.getString("state"));
account.setZip(rst.getString("zip"));
account.setCountry(rst.getString("country"));
account.setName(rst.getString("name"));
account.setEmail(rst.getString("email"));
account.setPhone(rst.getString("phone"));
account.setStatus(rst.getString("status"));
account.setPassword(rst.getString("password"));
}
con.close();
}
catch(SQLException e)
{
throw e;
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return account;
}
/**
* 用户登录,进行用户名和密码验证
*/
public boolean signon(String userId,String password)throws Exception
{
boolean validate=false;
try
{
con=DatabaseConnection.getConnection();
}
catch(Exception e)
{
throw e;
}
try
{
PreparedStatement pstmt=con.prepareStatement(signon);
pstmt.setString(1,userId);
pstmt.setString(2,password);
ResultSet rst=pstmt.executeQuery();
if(rst.next())
{
validate=true;
}
con.close();
}
catch(SQLException e)
{
throw e;
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(Exception e2)
{
}
}
return validate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -