📄 databaseofchatroom.java
字号:
package tryChat;
import java.sql.*;
import java.util.*;
public class DataBaseOfChatRoom { // 用于执行静态 SQL 语句并返回它所生成结果的对象
private static Statement sta; // 用于数据库操作的Statement
private static ResultSet rs; //表示数据库结果集的数据表
private Connection con;//与特定数据库的连接(会话)。
public static void main(String args[]){
//完成所有的方法和数据测试
DataBaseOfChatRoom trydata=new DataBaseOfChatRoom();
String str;
// trydata.setUserInformation("小明", "123456", "man", "Face", "Emile","QQ", "CountryAndProvince", "Favore");
//显示所有用户信息
// String[] str=trydata.getAllUserInformation();
// for(int i=0;i<str.length;i++){
// System.out.println(str[i]);
// }
//显示所有用户名单
// String[] string=trydata.getAllUserNames();
// for(int i=0;i<string.length;i++){
// System.out.println(string[i]);
// }
// str=trydata.getPassword("小明");
// trydata.setFavore("a1","nn");
// System.out.println(trydata.getFavore("a1"));
}
/**初始化各个数据*/
public DataBaseOfChatRoom(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载数据库驱动
con = DriverManager.getConnection("jdbc:odbc:ChatDataBase","", "");//连接数据库
sta = con.createStatement(); // 创建一个 Statement对象来将 SQL 语句发送到数据库
}catch(Exception e){System.out.println(e.toString());}
}
/**执行 修改,删除,插入等SQL语句*/
public void executeUpdate(String commandStr) {
try {
sta.executeUpdate(commandStr);
}catch(Exception e){System.out.println(e.toString());}
}
/**处理查询语句返回 ResultSet对象*/
public ResultSet executeQuery(String commandStr){
try
{
rs=sta.executeQuery(commandStr);
}catch(Exception e){System.out.println(e.toString());}
return rs;
}
/**添加新的用户*/
public void addUser(String Name,String Password,String Sex,String Face,String Emile,String QQ,String CountryAndProvince,String favore){
if(this.isAvailability(Name)){
String commandStr="insert into InformationOfUser(Name,Password,Sex,Face,Emile,QQ,CountryAndProvince,Favore) " +
"VALUES(\'" +Name+"\',"+"\'"+Password+"\',"+"\'"+Sex+"\',"+"\'"+Face+"\',"
+"\'"+Emile+"\',"+"\'"+QQ+"\',"+"\'"+CountryAndProvince+"\',"+"\'"+favore+"\')";
this.executeUpdate(commandStr);
}
}
/**删除用户*/
public void removeUser(String userName){
String commandStr="delete from InformationOfUser where Name=\'"+userName+"\'";
this.executeUpdate(commandStr);
}
/**返回指定用户个人信息所有信息*/
public String getUserInformation(String userName){
String str="";
str+="用户姓名:\t"+userName+"\n";
str+="用户密码:\t"+getPassword(userName)+"\n";
str+="用户性别:\t"+getSex(userName)+"\n";
str+="用户地址:\t"+getCountryAndProvince(userName)+"\n";
str+="用户邮箱:\t"+getEmile(userName)+"\n";
str+="用户QQ : \t"+getQQ(userName)+"\n";
str+="注册日期:\t"+getRegisterDate(userName)+"\n";
str+="个人留言:\t"+getFavore(userName)+"\n";
return str;
}
/**返回指定用户密码*/
public String getPassword(String userName){
String str="";
ResultSet rs=this.executeQuery("select Password from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("Password");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户性别*/
public String getSex(String userName){
String str="";
ResultSet rs=this.executeQuery("select Sex from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("Sex");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户头像*/
public String getFace(String userName){
String str="";
ResultSet rs=this.executeQuery("select Face from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("Face");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户邮箱*/
public String getEmile(String userName){
String str="";
ResultSet rs=this.executeQuery("select Emile from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("Emile");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户QQ*/
public String getQQ(String userName){
String str="";
ResultSet rs=this.executeQuery("select QQ from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("QQ");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户注册日期*/
public String getRegisterDate(String userName){
String str="";
rs=this.executeQuery("select RegisterDate from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("RegisterDate");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回指定用户所属地区*/
public String getCountryAndProvince(String userName){
String str="";
ResultSet rs=this.executeQuery("select CountryAndProvince from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("CountryAndProvince");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**返回用户留言*/
public String getFavore(String userName){
String str="";
ResultSet rs=this.executeQuery("select Favore from InformationOfUser where Name=\'"+userName+"\'");
try{
rs.next();
str=rs.getString("Favore");
rs.close();
}catch(Exception e){System.out.println(e.toString());}
return str;
}
/**获取所有用户姓名*/
public String[] getAllUserNames(){
Vector v=new Vector();
String str="";
try{
ResultSet rs=this.executeQuery("select Name from InformationOfUser ");
rs.next();
for(int i=0;i<rs.getRow();i++){
str=rs.getString("Name");
v.add(str);
rs.next();
}
rs.close();
}catch(Exception e){System.out.println(e.toString());}
String[] string=new String[v.size()];
//复制成对象数组
v.copyInto(string);
return string;
}
/**获取所有用户的用户信息*/
public String[] getAllUserInformation(){
Vector v=new Vector();
String str="";
try{
ResultSet rs=this.executeQuery("select* from InformationOfUser ");
rs.next();
for(int i=0;i<rs.getRow();i++){
str=rs.getString("Name")+"\t"+rs.getString("Password")+"\t"+rs.getString("Sex")
+"\t"+rs.getString("Face")+"\t"+rs.getString("Emile")+"\t"+rs.getString("QQ")
+"\t"+rs.getString("CountryAndProvince")+"\t"+rs.getString("Favore")+"\n";
rs.next();
v.add(str);
}
}catch(Exception e){System.out.println(e.toString());}
String[] string=new String[v.size()];
v.copyInto(string);
return string;
}
//Name,Password,Sex,Face,Emile,QQ,CountryAndProvince,Favore
/**修改用户信息*/
public void setUserInformation(String userName,String Password,String Sex,String Face,String Emile,String QQ,String CountryAndProvince,String Favore){
this.setPassword(userName, Password);
this.setSex(userName, Sex);
this.setFace(userName, Face);
this.setEmile(userName, Emile);
this.setQQ(userName, QQ);
this.setCountryAndProvince(userName, CountryAndProvince);
this.setFavore(userName, Favore);
}
/**修改用户密码*/
public void setPassword(String userName,String Password){
this.executeUpdate("update InformationOfUser set Password=\'"+Password+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户性别*/
public void setSex(String userName,String Sex){
this.executeUpdate("update InformationOfUser set Sex=\'"+Sex+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户头像*/
public void setFace(String userName,String Face){
this.executeUpdate("update InformationOfUser set Face=\'"+Face+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户邮箱*/
public void setEmile(String userName,String Emile){
this.executeUpdate("update InformationOfUser set Emile=\'"+Emile+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户QQ*/
public void setQQ(String userName,String QQ){
this.executeUpdate("update InformationOfUser set QQ=\'"+QQ+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户地址*/
public void setCountryAndProvince(String userName,String CountryAndProvince){
this.executeUpdate("update InformationOfUser set CountryAndProvince=\'"+CountryAndProvince+"\'"+"where Name=\'"+userName+"\'");
}
/**修改用户留言*/
public void setFavore(String userName,String Favore){
executeUpdate("update InformationOfUser set Favore=\'"+Favore+"\'"+"where Name=\'"+userName+"\'");
}
/**查看该用户列表中是否包含此用户.不包含return ture否则return false*/
public boolean isAvailability(String name){
String[] str=this.getAllUserNames();
for(int i=0;i<str.length;i++){
if(str[i].equals(name)){
return false;//有重名不可用
}
}
return true;
}
/**修改指定用户信息*/
public void setSpecialUserInfo(String userName,String Password,String Sex,String Face,
String Emile,String QQ,String CountryAndProvince,String favore ){
setPassword(userName, Emile);
setSex(userName, Sex);
setFace(userName, Face);
setEmile(userName, Emile);
setQQ(userName, QQ);
setCountryAndProvince(userName, CountryAndProvince);
setFavore(userName, favore);
}
/**释放资源,在程序未终止前勿使用*/
public void close(){
try
{
rs.close();
sta.close();
con.close();
}catch(Exception e)
{
System.out.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -