package com.ww.service; |
import java.sql.Connection; |
import java.sql.ResultSet; |
import java.sql.SQLException; |
import java.util.ArrayList; |
import com.mysql.jdbc.PreparedStatement; |
import com.ww.db.DBHelper; |
import com.ww.entity.Admin; |
public class AdminService { |
DBHelper dbHelper= new DBHelper(); |
//登录 |
public boolean login(String name,String pwd){ |
String sql= "select * from admin where username=? and pwd=?" ; |
Connection connection=dbHelper.getConnection(); |
try { |
PreparedStatement psd=(PreparedStatement)connection.prepareCall(sql); |
psd.setString( 1 , name); |
psd.setString( 2 ,pwd); |
ResultSet resultSet=psd.executeQuery(); |
if (resultSet.next()) { |
return true ; |
} |
|
} catch (SQLException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return false ; |
} |
|
//增加管理员 |
public boolean register(Admin admin){ |
String sql= "insert into admin values(null,?,?)" ; |
Connection connection=dbHelper.getConnection(); |
try { |
PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql); |
psd.setString( 1 , admin.getUser()); |
psd.setString( 2 , admin.getPwd()); |
int a=psd.executeUpdate(); |
System.out.print(a); |
if (a== 1 ) { |
return true ; |
} |
} catch (SQLException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return false ; |
} |
|
//删除管理员 |
public boolean deleteAdmin( int id){ |
String sql= "delete from admin where id=?" ; |
Connection connection=dbHelper.getConnection(); |
try { |
PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql); |
psd.setInt( 1 , id); |
int a=psd.executeUpdate(); |
if (a== 1 ) { |
return true ; |
} |
} catch (SQLException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return false ; |
} |
|
|
//修改管理员 |
public boolean updateAdmin(Admin admin){ |
String sql= "update admin set username=?,pwd=? where id=?" ; |
Connection connection=dbHelper.getConnection(); |
try { |
PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql); |
psd.setString( 1 , admin.getUser()); |
psd.setString( 2 , admin.getPwd()); |
psd.setInt( 3 , admin.getId()); |
int a=psd.executeUpdate(); |
if (a== 1 ) { |
return true ; |
} |
} catch (SQLException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return false ; |
} |
|
//查询所有admin的方法 |
public ArrayList<Admin> getAllAdmin(){ |
String sql= "select * from admin" ; |
Connection connection=dbHelper.getConnection(); |
ArrayList<Admin> admins= new ArrayList<Admin>(); |
try { |
PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql); |
ResultSet resultSet=psd.executeQuery(); |
while (resultSet.next()) { |
Admin admin= new Admin(); |
int id=resultSet.getInt( "id" ); |
String name=resultSet.getString( "username" ); |
String pwd=resultSet.getString( "pwd" ); |
admin.setId(id); |
admin.setUser(name); |
admin.setPwd(pwd); |
admins.add(admin); |
} |
} catch (SQLException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return admins; |
} |
} |
中级程序员
by: 东城 发表于:2016-05-12 09:26:37 顶(0) | 踩(0) 回复
回复评论