public class Account {
//这是一个vo类,是对Account的映射,它可以代表表中的一条记录
int accountid;
String name;
int remain;
public int getAccountid() {
return accountid;
}
public void setAccountid(int accountid) {
this.accountid = accountid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRemain() {
return remain;
}
public void setRemain(int remain) {
this.remain = remain;
}
public Account(int accountid, String name, int remain) {
super();
this.accountid = accountid;
this.name = name;
this.remain = remain;
}
public Account() {
super();
}
@Override
public String toString() {
return "Account [accountid=" + accountid + ", name=" + name + ", remain="
+ remain + "]";
}
public Account(String name, int remain) {
super();
this.name = name;
this.remain = remain;
}
public Account(int accountid, int remain) {
super();
this.accountid = accountid;
this.remain = remain;
}
}
package com.oracle.dao;
import java.util.List;
import com.oracle.vo.Account;
public interface AccountDao {
//数据访问对象的接口
//访问Account 增删改查(查询有两个)
//五个方法
//插入
public void insert(Account account);
//修改(根据)
public void update(Account account);
//删除
public void delete(int accountid);
//查询一个;
public Account getAccountById(int accountid);
//查询所有的账号
public List<Account> getAll();
//分页查询
public List<Account>getAllForPage(int pageSize,int pageNumber);
}
package com.oracle.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.oracle.vo.Account;
public class AccountDaoImpl implements AccountDao {
//将通用的操作封装起来
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
@Override
public void insert(Account account) {
//1.获取连接
Connection conn=this.getConnection();
PreparedStatement ps=null;
String sql="insert into account values(null,?,?)";
try{
ps=conn.prepareStatement(sql);
ps.setString(1, account.getName());
ps.setInt(2, account.getRemain());
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void update(Account account) {
Connection conn=this.getConnection();
PreparedStatement ps=null;
String sql="update account set name=?,remain=? where accountid=?";
try{
ps=conn.prepareStatement(sql);
ps.setString(1, account.getName());
ps.setInt(2, account.getRemain());
ps.setInt(3, account.getAccountid());
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void delete(int accountid) {
Connection conn=this.getConnection();
PreparedStatement ps=null;
try
{
ps=conn.prepareStatement("delete from account where accountid=?");
ps.setInt(1, accountid);
ps.execute();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public Account getAccountById(int accountid) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
Account account=null;
try{
conn=this.getConnection();
ps=conn.prepareStatement("select * from account where accountid=?");
ps.setInt(1,accountid);
//查询结果
rs=ps.executeQuery();
if(rs.next()){
//将记录转换成vo
account=new Account(rs.getInt(1),rs.getString(2),rs.getInt(3));
//account=new Account();
//account.setAccountid(rs.getInt(1));
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close(conn, ps, rs);
}
return account;
}
@Override
public List<Account> getAll() {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Account>list=new ArrayList<Account>();
try {
conn=this.getConnection();
ps=conn.prepareStatement("select * from account ");
rs=ps.executeQuery();
while(rs.next()){
Account a=new Account();
a.setAccountid(rs.getInt(1));
a.setName(rs.getString(2));
a.setRemain(rs.getInt(3));
list.add(a);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
this.close(conn, ps, rs);
}
return list;
}
private void close(Connection conn,Statement ps){
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void close(Connection conn,Statement ps,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.close(conn, ps);
}
@Override
public List<Account> getAllForPage(int pageSize, int pageNumber) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Account>list=new ArrayList<Account>();
try {
conn=this.getConnection();
String sql="select * from account limit ?,?";
ps=conn.prepareStatement(sql);
ps.setInt(1, (pageNumber-1)*pageSize);
ps.setInt(2, pageSize);
rs=ps.executeQuery();
while(rs.next()){
Account a=new Account();
a.setAccountid(rs.getInt(1));
a.setName(rs.getString(2));
a.setRemain(rs.getInt(3));
list.add(a);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
this.close(conn, ps, rs);
}
return list;
}
}
package com.oracle.test;
import java.util.List;
import java.util.Scanner;
import com.oracle.dao.AccountDao;
import com.oracle.dao.AccountDaoImpl;
import com.oracle.vo.Account;
public class TestAccount {
public static void main(String[] args) {
AccountDao dao=new AccountDaoImpl();
//dao.insert(new Account("利好",6555));
// dao.delete(5);
Scanner s=new Scanner(System.in);
// System.out.println("要修改的账号");
// int accountid=s.nextInt();
// System.out.println("姓名");
// String name=s.next();
// System.out.println("余额");
// int remain=s.nextInt();
//
// Account a=new Account(accountid,name,remain);
//
// dao.update(a);
// System.out.println("要查询的账号");
// int accountid=s.nextInt();
// Account a=dao.getAccountById(accountid);
// System.out.println(a);
// List<Account>list=dao.getAll();
// for(Account a:list){
// System.out.println(a);
// }
List<Account>list=dao.getAllForPage(2, 1);
for(Account a:list){
System.out.println(a);
}
}
}