package com.online.database_option;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.online.tool.MD5;
import com.online.ui_manager.Manager.completeListener;
public class DBoption_search
{
//驱动名、URL、user name、password
String driver=com.online.util.Data.driver;
String url=com.online.util.Data.url;
String user=com.online.util.Data.user;
String password=com.online.util.Data.password;
String userId=com.online.util.Data.userId;
//构造方法
public DBoption_search()
{
}
//用于查询基本信息
// public ArrayList<Object> aStaffAll(String id)
// {
// //结果储存
// ArrayList<Object> results=new ArrayList<>();
//
// try
// {
// //加载驱动
// Class.forName(driver);
// //连接数据库
// Connection connection=DriverManager.getConnection(url,user,password);
//
// //执行sql语句
// PreparedStatement ps =connection.prepareStatement("select * from online_staff where id=? and leavedate is null");
// ps.setString(1, id);
// ResultSet result= ps.executeQuery();
//
// String name,sex,phone,qq,email,district,department,position = "";
// Date joindate;
// while(result.next())
// {
// //从结果集中得到各种结果
// name=result.getString("name");
// sex=result.getString("sex");
// phone=result.getString("phone");
// qq=result.getString("qq");
// email=result.getString("email");
// district=result.getString("district");
// joindate=result.getDate("joindate");
//
// result
//
// }
//
// result.close();
// connection.close();
// }
// catch (Exception e)
// {
// System.out.println("aStaffAll select error!");
// }
//
// return result;
// }
//用id查询部门编号----------------------------------------------------------------------------------------
public String IdToDepartmentId(String id)
{
String did="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//查询sql
PreparedStatement ps =connection.prepareStatement("select d_id from online_department_in where id=?");
ps.setString(1, id);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
did=result1.getString("d_id");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("id to departmentid select error");
}
return did;
}
//用id查职务编号------------------------------------------------------------------------------------------
public String IdToPositionId(String id)
{
String pid="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//查询sql
PreparedStatement ps =connection.prepareStatement("select p_id from online_position_in where id=?");
ps.setString(1, id);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
pid=result1.getString("p_id");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("id to positionId select error");
}
return pid;
}
//用部门名字查询部门编号-------------------------------------------------------------------------------------
public String DepartmentToDepartmentId(String department)
{
String did="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//保存名字
PreparedStatement ps =connection.prepareStatement("select d_id from online_department where d_name=?");
ps.setString(1, department);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
did=result1.getString("d_id");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("department select error");
}
return did;
}
//用部门编号查询部门名字-------------------------------------------------------------------------------------
public String DepartmentIdToDepartmetn(String d_id)
{
String d_name="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//保存名字
PreparedStatement ps =connection.prepareStatement("select d_name from online_department where d_id=?");
ps.setString(1, d_id);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
d_name=result1.getString("d_name");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("did to department select error");
}
return d_name;
}
//用职位名字查职位编号--------------------------------------------------------------------------------------
public String PositionToPositionId(String position)
{
String pid="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//保存名字
PreparedStatement ps =connection.prepareStatement("select p_id from online_position where p_name=?");
ps.setString(1, position);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
pid=result1.getString("p_id");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("position select error");
}
return pid;
}
//用职位编号查职位名字----------------------------------------------------------------------------------
public String PositionIdToPosition(String p_id)
{
String p_name="";
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
//保存名字
PreparedStatement ps =connection.prepareStatement("select p_name from online_position where p_id=?");
ps.setString(1, p_id);
ResultSet result1=ps.executeQuery();
while(result1.next())
{
p_name=result1.getString("p_name");
}
result1.close();
connection.close();
}
catch (Exception e)
{
System.out.println("position select error");
}
return p_name;
}
//登陆-------------------------------------------------------------------------------------
public boolean Login(String id,String pass)
{
boolean isLogin = false;
try
{
//加载驱动
Class.forName(driver);
//连接数据库
Connection connection=DriverManager.getConnection(url,user,password);
if(!ExistID(id))
{
JOptionPane.showMessageDialog(null, "不好意思,学号有误~", "不能这样做~", JOptionPane.NO_OPTION);
return false;
}
else
{
//保存名字
PreparedStatement ps =connection.prepareStatement("select password from online_staff where id=?");
ps.setString(1, id);
ResultSet result1=ps.executeQuery();
//判断是否空
while(result1.next())
{
String code=result1.getString("password");
MD5 md5=new MD5();
String inputCode=md5.GetMD5Code(pass);
if(inputCode.equals(code))
{
System.out.println();
return true;
}
else
{
System.out.println(inputCode);
JOptionPane.showMessageDialog(null, "不好意思,密码有误~", "不能这样做~", JOptionPane.NO_OPTION);
}
}
//关流
result1.close();
connection.close();