用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

单表增删改查

2016-05-10 作者: 画船听雨眠举报

[java]代码库

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;
	}

}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...