package com.leec.demo.model.DAOImplJDBC.dao;
import java.sql.*;
import java.util.*;
import com.leec.demo.model.dto.*;
/**
*kingbill 2006 3.21 all rights reserved
*������ strutsbook@126.com
*��ݿ���ֱ������ ����0.8��
*/
/*
*/
//adminDAO++++++++++++++++++++++++++++++++++++++++++++++++++
public class AdminDAO {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/demo";
String username = "root";
String password = "root";
//CreateByDTO------------------------------------------------
public void createAdmin(AdminDTO admin)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
String sql="insert into admin (a_id,a_username,a_pass) values ("
+ "'" + admin.getA_id() + "'" + ","
+ "'" + admin.getA_username() + "'" + ","
+ "'" + admin.getA_pass() + "'"
+")";
stmt = con.createStatement();
stmt.execute(sql);
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
}
}
//UpdateByDTO------------------------------------------------
public void updateAdmin(AdminDTO admin)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
String sql = "update admin set "
+ "a_username ='" + admin.getA_username() + "',"
+ "a_pass ='" + admin.getA_pass() + "'"
+ " where a_id = '" + admin.getA_id() + "'"
;
stmt = con.createStatement();
stmt.executeUpdate(sql);
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
}
}
//UpdateByDTOs------------------------------------------------
public void updateAdmins(Vector admins)
{
Iterator it = admins.iterator();
while(it.hasNext())
{
AdminDTO admin = (AdminDTO)it.next();
updateAdmin(admin);
}
}
//RemoveByPrimaryKey------------------------------------------------
public void removeByPrimaryKey(java.lang.String _a_id)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
String sql = "delete from admin where a_id = '" + _a_id + "'";
stmt = con.createStatement();
stmt.execute(sql);
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
}
}
//RemoveByDTO------------------------------------------------
public void removeAdmin(AdminDTO admin)
{
removeByPrimaryKey(admin.getA_id());
}
//RemoveByDTOs------------------------------------------------
public void removeAdmins(Vector admins)
{
Iterator it = admins.iterator();
while(it.hasNext())
{
AdminDTO admin = (AdminDTO)it.next();
removeAdmin(admin);
}
}
//findByPrimaryKey------------------------------------------------
public AdminDTO findByPrimaryKey(java.lang.String _a_id)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
AdminDTO admin = new AdminDTO();
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String sql = "select * from admin where a_id = '" + _a_id + "'";
rs = stmt.executeQuery(sql);
while(rs.next())
{
admin.setA_id(rs.getString("a_id"));
admin.setA_username(rs.getString("a_username"));
admin.setA_pass(rs.getString("a_pass"));
}
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
return admin;
}
}
public List findAll()
{List<AdminDTO> ls=new ArrayList<AdminDTO>();
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
AdminDTO admin = null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String sql = "select * from admin";
rs = stmt.executeQuery(sql);
while(rs.next())
{
admin = new AdminDTO();
admin.setA_id(rs.getString("a_id"));
admin.setA_username(rs.getString("a_username"));
admin.setA_pass(rs.getString("a_pass"));
ls.add(admin);
}
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
return ls;
}
}
public List findByusername(java.lang.String _a_username)
{List<AdminDTO> ls=new ArrayList<AdminDTO>();
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
AdminDTO admin = null;
try{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String sql = "select * from admin where a_username = '" + _a_username + "'";
rs = stmt.executeQuery(sql);
while(rs.next())
{
admin = new AdminDTO();
admin.setA_id(rs.getString("a_id"));
admin.setA_username(rs.getString("a_username"));
admin.setA_pass(rs.getString("a_pass"));
ls.add(admin);
}
}
catch(Exception sqlex1)
{
sqlex1.printStackTrace();
}
finally
{
if (con != null) {
try {
con.close();
}
catch(SQLException sqlex2)
{
sqlex2.printStackTrace();
}
}
return ls;
}
}
}