package com.bn.serverinfo;//声明包语句
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DBUtil{ //创建主类
public static Connection getConnection(){ //编写与数据库建立连接的方法
Connection con = null; //声明连接
try{
Class.forName("org.gjt.mm.mysql.Driver"); //声明驱动
con = DriverManager.getConnection("jdbc:mysql://localhost/orderdish?useUnicode=true&characterEncoding=UTF-8","root","");//得到连接(数据库名,编码形式,数据库用户名,数据库密码)
}catch(Exception e){
e.printStackTrace(); //捕获异常
}
return con; //返回连接
}
//根据员工的姓名得到id
public static String getWorkeridByname(String string){
Connection con = getConnection();//与数据库建立连接
Statement st = null;//创建接口
ResultSet rs = null;//结果集
String str = null;//字符串常量
try{
st = con.createStatement();//创建一个对象来将SQL语句发送到数据库
String task = "select w_id from WorkerInfo where w_name='"+string+"';";//编写SQL语句
rs = st.executeQuery(task);//执行SQL语句
rs.next(); //遍历执行
str=rs.getString(1); //将查询得到的员工ID放入字符串常量
}
catch(Exception e){ //捕获异常,返回空值
e.printStackTrace();
return null;
}
finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}//结果集
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return str; //返回员工ID
}
//得到计量单位的信息
public static List<String[]> getCU(){
Connection con = getConnection();//与数据库建立连接
Statement st = null;//创建接口
ResultSet rs = null;//结果集
List<String[]>ll=new ArrayList<String[]>();//创建存放数据的列表
try{
st=con.createStatement();//创建一个对象来将SQL语句发送到数据库
String sql="select unit_id,unit_name,unit_remark from CountUnit where unit_delflg='"+0+"'";//编写SQL语句
rs=st.executeQuery(sql);//执行SQL语句
while(rs.next()){ //循环遍历结果集
String[] s=new String[3];
for(int i=0;i<s.length;i++){
s[i]=rs.getString(i+1); //将得到的信息放入字符串数组
}
ll.add(s); //将字符串数据添加到列表
}
}catch(Exception e){
e.printStackTrace(); //捕获异常
}
finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return ll;
}
////得到计量单位的最大编号
public static String getCUMaxNO(){
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
String str=new String();
try{
st=con.createStatement();
String sql="select max(unit_id) from countunit";
rs=st.executeQuery(sql);
rs.next();
if(rs.getString(1)==null){
str=String.valueOf(0);
}
else{
str=rs.getString(1);
}
}
catch(Exception e){
e.printStackTrace();
}finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return str;
}
//更新计量单位
private static Object UPDATECOUNTUNIT=new Object();
public static String updateCU(String[] getmsg){
synchronized(UPDATECOUNTUNIT){ //方法加锁
Connection con = getConnection();
Statement st = null;
String idnum[]=getCountUnitID();
try{
int i;
st = con.createStatement();
for( i=0;i<idnum.length;i++){ //若是已有的计量单位,就更改
if(getmsg[1].equals(idnum[i])){
String sql = "update countunit set unit_name= '"+getmsg[2]+"',unit_remark= '"+getmsg[3]+"' where unit_id="+getmsg[1];
st.executeUpdate(sql);
break;
}
}
if(i==idnum.length){ //若不是已有的,就插入
String sql="insert into countunit(unit_id,unit_name,unit_remark) values('"+getmsg[1]+"','"+getmsg[2]+"','"+getmsg[3]+"')";
st.executeUpdate(sql);
}
}catch(Exception e){
e.printStackTrace() ;
return null;
}finally{
try{st.close();}catch(Exception e) {e.printStackTrace();}
try{con.close();}catch(Exception e) {e.printStackTrace();}
}
return "ok";
}
}
//得到计量单位的编号
private static String[] getCountUnitID(){
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
int count=getCount("countunit");
String[] ll=new String[count];
try{
st=con.createStatement();
String sql="select unit_id from countunit";
rs=st.executeQuery(sql);
int k=0;
System.out.println(rs.getRow()); //打印编号
while(rs.next()){
System.out.println(rs.getString(1));
ll[k]=rs.getString(1);
System.out.println(ll[k]+"k="+k);
k++;
}
}catch(Exception e){
e.printStackTrace();
}
finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return ll;
}
//删除计量单位信息
private static Object DELCOUNTUNIT=new Object();
public static String delCU(String id) {
synchronized(DELCOUNTUNIT){ //加锁
Connection con = getConnection();
Statement st = null;
try{
st = con.createStatement();
String sql = "update countunit set unit_delflg='"+1+"' where unit_id="+id;
st.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace() ;
return null;
}finally{
try{st.close();}catch(Exception e) {e.printStackTrace();}
try{con.close();}catch(Exception e) {e.printStackTrace();}
}
return "ok";
}
}
//根据计量单位id得到名称
public static String getCUnameById(String string) {
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
String str = null;
try{
st = con.createStatement();
String task = "select unit_name from CountUnit where unit_id='"+string+"';";
rs = st.executeQuery(task);
rs.next();
str=rs.getString(1);
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return str;
}
//根据员工id得到名称
public static String getWorkernameById(String string){
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
String str = null;
try{
st = con.createStatement();
String task = "select w_name from WorkerInfo where w_id='"+string+"';";
rs = st.executeQuery(task);
rs.next();
str=rs.getString(1);
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
try {st.close();} catch (SQLException e) {e.printStackTrace();}
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
return str;
}
//根据计量单位名称得到id
public static String getCUidByname(String string) {
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
String str = null;
try{
st = con.createStatement();
Stri