java权限获取代码_java权限的增删改查 示例代码

【实例简介】实现java权限的增删改查

【实例截图】

【核心代码】

package com.fangjian.dao.role.impl;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import com.fangjian.dao.role.IbookRoleDao;

import com.fangjian.po.role.IbookRole;

import com.fangjian.util.DbUtil;

/**

* IbookRoleDao 接口的实现

* @author fangjian

*

*/

public class IbookRoleDaoImpl implements IbookRoleDao {

//数据库连接工具

private DbUtil dbutil = null;

//性能最好

private PreparedStatement ps=null;

//连接

private Connection conn=null;

private final String findById="select * from IBOOK_ROLE where id = ?";

private final String deleteByID="delete from IBOOK_ROLE where id = ?";

private final String insert="insert into IBOOK_ROLE(ROLENAME,REMARK,ISADMIN,OWNLENGTH) values(?,?,?,?)";

private final String edit="update IBOOK_ROLE set ROLENAME=?,REMARK = ?,ISADMIN=?,OWNLENGTH=? where id =?";

private final String findAll="select * from IBOOK_ROLE";

private final String deleteAll="delete from IBOOK_ROLE";

@Override

public IbookRole findById(int id) {

conn = this.getMysqlJdbcConnection();

IbookRole vo = null;

try {

ps = conn.prepareStatement(findById);

ps.setInt(1, id);

ResultSet rs = ps.executeQuery();

while(rs.next()){

vo= new IbookRole();

vo.setId(rs.getInt("id"));

vo.setRolename(rs.getString("rolename"));

vo.setRemark(rs.getString("remark"));

vo.setIsadmin(rs.getInt("isadmin"));

vo.setOwnlenght(rs.getInt("OWNLENGTH"));

}

} catch (SQLException e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return vo;

}

@Override

public boolean deleteByID(int id) {

boolean b = false;

conn =this.getMysqlJdbcConnection();

try{

ps = conn.prepareStatement(deleteByID);

ps.setInt(1, id);

//返回影响的行数

int i = ps.executeUpdate();

if(i>0){

b = true;

}

}catch (Exception e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return b;

}

@Override

public boolean insert(IbookRole t) {

boolean b = false;

conn = this.getMysqlJdbcConnection();

try{

ps = conn.prepareStatement(insert);

ps.setString(1, t.getRolename());

ps.setString(2, t.getRemark());

ps.setInt(3, t.getIsadmin());

ps.setInt(4, t.getOwnlenght());

int i = ps.executeUpdate();

if(i>0){

b = true;

}

}catch (Exception e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return b;

}

@Override

public boolean edit(IbookRole t) {

boolean b = false;

conn = this.getMysqlJdbcConnection();

try{

ps = conn.prepareStatement(edit);

ps.setString(1, t.getRolename());

ps.setString(2, t.getRemark());

ps.setInt(3, t.getIsadmin());

ps.setInt(4, t.getOwnlenght());

ps.setInt(5, t.getId());

int i = ps.executeUpdate();

if(i>0){

b = true;

}

}catch (Exception e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return b;

}

@Override

public List findAll() {

List listroles = new ArrayList();

conn = this.getMysqlJdbcConnection();

IbookRole vo = null;

try {

ps = conn.prepareStatement(findAll);

ResultSet rs = ps.executeQuery();

while(rs.next()){

vo= new IbookRole();

vo.setId(rs.getInt("id"));

vo.setRolename(rs.getString("rolename"));

vo.setRemark(rs.getString("remark"));

vo.setIsadmin(rs.getInt("isadmin"));

vo.setOwnlenght(rs.getInt("OWNLENGTH"));

listroles.add(vo);

}

} catch (SQLException e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return listroles;

}

@Override

public boolean deleteAll() {

boolean b = false;

conn =this.getMysqlJdbcConnection();

try{

ps = conn.prepareStatement(deleteAll);

//返回影响的行数

int i = ps.executeUpdate();

if(i>0){

b = true;

}

}catch (Exception e) {

dbutil.clearUp(conn);

e.printStackTrace();

}finally{

dbutil.clearUp(conn);

}

return b;

}

/**

* 获取conn链接

* @return

*/

public Connection getMysqlJdbcConnection(){

dbutil = new DbUtil();

conn = dbutil.getConn();

return conn;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的基于Spring Boot的增删改查代码示例: 1. 创建实体类 ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // getters and setters } ``` 2. 创建数据访问接口 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 3. 创建服务类 ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getAllUsers() { return userRepository.findAll(); } public User getUserById(Long id) { return userRepository.findById(id).orElse(null); } public User addUser(User user) { return userRepository.save(user); } public User updateUser(Long id, User user) { User existingUser = userRepository.findById(id).orElse(null); if (existingUser != null) { existingUser.setName(user.getName()); existingUser.setEmail(user.getEmail()); userRepository.save(existingUser); } return existingUser; } public void deleteUser(Long id) { userRepository.deleteById(id); } } ``` 4. 创建控制器类 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("") public List<User> getAllUsers() { return userService.getAllUsers(); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } @PostMapping("") public User addUser(@RequestBody User user) { return userService.addUser(user); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { return userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } ``` 以上代码实现了一个简单的增删改查功能,你可以在控制器类的接口上添加相应的注解来限制访问权限。同时,你可以添加异常处理程序来处理异常情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值