📄 usermgr.java
字号:
package server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* Created on 2005-9-7
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author 09003112小组
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UserMgr
{
private int row=0;
//主方法供调试时用
/*public static void main(String args[])
{
UserMgr UM=new UserMgr();
//int h=UM.getRow();
//System.out.println(h);
//UM.updateInfor_Price("star warIII",1000000);
//UM.updateInfor_M("Tom",200);
//UM.insertInfor("GOD",378612);
//UM.updateInfor_EndT(1000,"GOD");
//UM.deleteInfor("Tim");
//UM.selectInfor_PS("Tim");
//UM.showInfor_Name(10);
//if(UM.userExist("sun"))
//System.out.println(" jhafa");
//UM.showUserOnLine_Name(10);
//UM.selectInfor_OL("aaaa");
//UM.updateInfor_PS(1000,"Nicholas");
}
*/
public int getRow()
{
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="select * from User";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
row++;
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:getRow"+ e);
}
int aa=row;
row=0;
return aa;
}
public boolean userExist(String name) //判断用户名是否存在
{
boolean userExist=false;
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="select * from User";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
if(rs.getString("Name").equals(name))
{
userExist=true;
break;
}
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:Judge userName Exist"+ e);
}
return userExist;
}
public String[] showInfor_Name(int row) //显示所有数据库记录(用户名)
{
String[] userName=new String[row];
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="SELECT * FROM User";
ResultSet rs=stmt.executeQuery(sql);
if(rs!=null)
{ int i=0;
while(rs.next())
{
userName[i]=rs.getString(1);
//System.out.println("姓名:"+userName[i]);
i++; }
rs.close();
stmt.close();
con.close();
}}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:showInfor_Name"+ e);
}
return userName ;
}
public long[] showInfor_Money(int row) //显示所有数据库记录(金钱)
{
long userMoney[]=new long [row];
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="SELECT * FROM User";
ResultSet rs=stmt.executeQuery(sql);
if(rs!=null)
{ int i=0;
while(rs.next())
{
userMoney[i]=rs.getLong(3);
//System.out.println("姓名:"+userMoney[i]);
i++; }
rs.close();
stmt.close();
con.close();
}
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:showInfor_Money"+ e);
}
return userMoney ;
}
public String[] showUserOnLine_Name(int row) //显示在线用户信息(用户名)
{
String[] userName=new String[row];
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="SELECT * FROM User WHERE onLine=1";
ResultSet rs=stmt.executeQuery(sql);
if(rs!=null)
{
int i=0;
while(rs.next())
{
userName[i]=rs.getString(1);
//System.out.println("姓名:"+userName[i]);
i++;
}
for(;i<row;i++)
userName[i]="administrator";
rs.close();
stmt.close();
con.close();
}
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:showUserOnline_Name"+ e);
}
return userName;
}
public long[] showUserOnLine_Money(int length)//显示在线用户信息(金钱)
{
long[] userMoney=new long[length];
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="SELECT * FROM User WHERE onLine=1";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
String name=rs.getString(1);
long money=rs.getLong(3);
//System.out.println("姓名:"+name+" 金钱: "+money);
}
rs.close();
stmt.close();
con.close();
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:showUserOnline_Money"+ e);
}
return userMoney;
}
public void insertInfor(String name,long password)//添加用户记录
{
int i=-1;
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql2="INSERT INTO User"+" VALUES('"+name+"',"+password+","+10000+","+0+","+0+","+0+")";
i=stmt.executeUpdate(sql2);
stmt.close();
con.close();
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:insertInfor"+ e);
}
//System.out.println("数据添加到第"+i+"行");
}
public void deleteInfor(String name) //删除用户记录
{
int i=-1;
try
{//加载驱动程序,此处为JDBC-ODBC桥
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//指定连接的ODBC数据源为:"management"
String source="jdbc:odbc:management";
//通过驱动程序管理器建立与数据源的连接
Connection con=DriverManager.getConnection(source);
//创建执行查询的Statement对象
Statement stmt=con.createStatement();
String sql="DELETE FROM User WHERE Name= '"+name+"'";
i=stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch(ClassNotFoundException e)
{
System.out.println("error:ClassNotFound"+e);
}
catch(SQLException e)
{
System.out.println("error:deleteInfor"+ e);
}
//System.out.println("deleteInfor,done!!");
}
public void updateInfor_PS(long newPassword, String name)//修改密码
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -