📄 connectsql.java
字号:
package Java.ICQ;
import java.sql.*;
import java.util.*;
public class ConnectSQL
{
Connection con;
PreparedStatement prepare;
Statement st;
ResultSet rslt;
public ConnectSQL()
{
}
//连接数据库
public void connectDBSQL()
{
String dirver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sever";
String username="sa";
String password="";
try
{
Class.forName(dirver);
System.out.println("装载数据库驱动成功");
con=DriverManager.getConnection(url,username,password);
System.out.println("连接数库成功");
}
catch(SQLException e)
{
System.out.println("装载数据库驱动失败");
}
catch(ClassNotFoundException nfe)
{
System.out.println("没有找到驱动");
}
}
//用户登陆查询数据库函数
public String LoginSQL(int username,String password)
{
String sql="select nickname,password from icq where icqno=?";
String result=null;
//准备从数据库选择呢称和密码
try
{
prepare = con.prepareStatement(sql);//设定数据库查寻条件
prepare.setInt(1,username);//设定参数
rslt=prepare.executeQuery();//执行数据库查寻
System.out.println("查询成功");
if(rslt.next())
{//以下比较输入的号码于密码是否相同
String pass=rslt.getString("password").trim();
result=pass;
System.out.println(pass);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
closeSQL();
}
return result;
}
//用户登陆更新
public void updateSQL(String address,int username)
{
String setip="update icq set ip=? where icqno=?";
try
{
prepare=null;
prepare = con.prepareStatement(setip);
prepare.clearParameters();
prepare.setString(1,address);
prepare.setInt(2,username);
int set=prepare.executeUpdate();
System.out.println(set);
System.out.println("更新IP地址成功");
//*************ipaddress
//并且更新数据库用户为在线
String status="update icq set status=1 where icqno=?";
prepare = con.prepareStatement(status);
prepare.clearParameters();
prepare.setInt(1,username);
int set2=prepare.executeUpdate();
System.out.println(set2);
System.out.println("更新在线用户成功");
}
catch(Exception e)
{
System.out.println("查询失败!");
}
finally
{
closeSQL();
}
}
//注册用户插入数据库
public String insertSQL(String nickname,String password,String email,String sex,String info,String place,int picindex)
{
String rsinsert="registerError";
String newsql="insert into icq(nickname,password,Status,email,sex,info,place,pic) values(?,?,?,?,?,?,?,?)";
try
{
prepare = con.prepareStatement(newsql);
prepare.clearParameters();
prepare.setString(1,nickname);
prepare.setString(2,password);
prepare.setString(3,"0");
prepare.setString(4,email);
prepare.setString(5,sex);
prepare.setString(6,info);
prepare.setString(7,place);
prepare.setInt(8,picindex);
int r3=prepare.executeUpdate();//执行数据库添加
System.out.println(r3);
if(r3==1)
{
prepare = con.prepareStatement("select number=max(Icqno) from icq");
prepare.clearParameters();
rslt = prepare.executeQuery();
if(rslt.next())
{
System.out.println(rslt.getInt("number"));
rsinsert = String.valueOf(rslt.getInt("number"))+"registerSucceed";
System.out.println(rsinsert);
}
}
else
{
rsinsert ="registerError";
}
}
catch(Exception e)
{
System.out.println("查询失败!");
}
finally
{
closeSQL();
}
return rsinsert;
}
//查找好友
public Vector findFriend()
{
//以下连接数据库,并且返回其他用户的呢称,性别,籍贯,个人资料等信息
String find="select * from icq";
Vector vector=null;
try
{
//查询其他用户的呢称,性别,籍贯,个人资料等信息
st=con.createStatement();
rslt=st.executeQuery(find);
vector = new Vector();
while(rslt.next())
{
Friend friendclass= new Friend();
friendclass.friendNickname=rslt.getString("nickname");
friendclass.friendSex=rslt.getString("sex");
friendclass.friendPlace=rslt.getString("place");
friendclass.friendIp=rslt.getString("ip");
friendclass.friendEmail=rslt.getString("email");
friendclass.picInfo=rslt.getString("info");
friendclass.friendIcqno=rslt.getInt("icqno");
friendclass.friendPic=rslt.getInt("pic");
boolean status=rslt.getBoolean("status");
if (status)
friendclass.friendStatus="1";
else
friendclass.friendStatus="0";
System.out.println(friendclass.friendNickname);
System.out.println(friendclass.friendSex);
System.out.println(friendclass.friendPlace);
System.out.println(friendclass.friendIp);
System.out.println(friendclass.friendEmail);
System.out.println(friendclass.picInfo);
System.out.println(friendclass.friendIcqno);
System.out.println(friendclass.friendPic);
System.out.println(friendclass);
vector.add(friendclass);
}
}
catch(Exception e)
{
System.out.println("查询失败!");
}
finally
{
closeSQL();
}
return vector;
}
//上线时读取好友的资料
public Vector readFriend(int icqno)
{
Vector vector = new Vector();
String friend="select friend from friend where icqno=?";
String friendinfo="select * from icq where icqno=?";
try
{
prepare = con.prepareStatement(friend);
prepare.setInt(1,icqno);
rslt=prepare.executeQuery();
Vector friendno=new Vector();//该矢量保存好友号码
while(rslt.next())
{
friendno.add(new Integer(rslt.getInt(1)));//把好友添加到Vector对像中
}
for(int i=0;i<friendno.size();i++)//根据好友的id查询好友的资料
{
rslt=null;
prepare = con.prepareStatement(friendinfo);
prepare.clearParameters();
prepare.setObject(1,friendno.get(i));
rslt=prepare.executeQuery();
boolean status;
while(rslt.next())
{
Friend friendclass= new Friend();
friendclass.friendNickname=rslt.getString("nickname");
friendclass.friendIcqno=rslt.getInt("icqno");
friendclass.friendSex=rslt.getString("sex");
friendclass.friendIp=rslt.getString("ip");
status=rslt.getBoolean("status");
if (status)
friendclass.friendStatus="1";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -