📄 operatormanager.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.mobile.mode;import com.mobile.util.DBUtil;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * * @author st */public class OperatorManager { private OperatorManager() { } private static OperatorManager manager = null; public static OperatorManager getInstance() { if (manager == null) { manager = new OperatorManager(); } return manager; }//..........................//1 通过//0 密码错//-1 用户不存在 public int login(String id, String pwd) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int is = -1; try { String sql = "select Operator_Pwd from TOperator where Operator_ID=?"; conn = DBUtil.getConnection(); stmt = conn.prepareStatement(sql); stmt.setString(1, id); rs = stmt.executeQuery(); if (rs.next()) { String rpwd = rs.getString("Operator_Pwd"); if (pwd.equals(rpwd)) { is = 1; } else { is = 0; } } else { is = -1; } return is; } catch (SQLException e) { e.printStackTrace(); return -1; } finally { DBUtil.close(null, stmt, conn); } } public boolean isAdmin(String id) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; String is = ""; try { String sql = "select is_admin from TOperator where Operator_ID=?"; conn = DBUtil.getConnection(); stmt = conn.prepareStatement(sql); stmt.setString(1, id); rs = stmt.executeQuery(); if (rs.next()) { is = rs.getString(1); } if ("Y".equals(is)) { return true; } else { return false; } } catch (SQLException e) { e.printStackTrace(); return false; } finally { DBUtil.close(null, stmt, conn); } } public boolean save(Operator o) { Connection conn = null; PreparedStatement stmt = null; int rs = -1; String sql = "insert into TOperator values(?,?,?,?)"; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement(sql); stmt.setString(1, o.getId()); stmt.setString(2, o.getName()); stmt.setString(3, o.getPwd()); stmt.setString(4, o.isIsadmin() ? "Y" : "N"); rs = stmt.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace(); }finally{ DBUtil.close(null, stmt, conn); } if(rs == 0){ return false; }else{ return true; } }public static void main(String[] args){ Operator o = new Operator(); o.setId("1"); o.setIsadmin(true); o.setName("tom"); o.setPwd("tom"); System.out.println(OperatorManager.getInstance().save(o));}/** * true 没有人用 * false 有人用 * @param id * @return */public boolean checkId(String id){ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int irs = -1; String sql = "select count(*) from TOperator where Operator_ID = ?"; boolean is = false; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement(sql); stmt.setString(1, id); rs = stmt.executeQuery(); if(rs.next()){ irs = rs.getInt(1); } } catch (SQLException ex) { ex.printStackTrace(); }finally{ DBUtil.close(null, stmt, conn); } if(irs == 0){ return true; }else{ return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -