一、客户端代码
package 客户端程序;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException, InterruptedException {
DatagramSocket ds = new DatagramSocket(6666);
@SuppressWarnings("resource")
Scanner inputs = new Scanner(System.in);
InetAddress address = InetAddress.getByName("DESKTOP-H1RVE5U");
int port = 8888;
while(true)
{
System.out.println("请输入ID:");
String id = inputs.nextLine();
System.out.println("请输入密码:");
String pw = inputs.nextLine();
byte[] bysid = id.getBytes();
int lenid = bysid.length;
byte[] byspw = pw.getBytes();
int lenpw = byspw.length;
DatagramPacket did = new DatagramPacket(bysid,lenid,address,port);
DatagramPacket dpw = new DatagramPacket(byspw,lenpw,address,port);
ds.send(did);
ds.send(dpw);
System.out.println("\n");
System.out.println("等待返回登录信息......\n");
Thread.sleep(1000);
//设置一个1000ms的响应时间
/
//接收服务器返回来的信息
byte[] byteret = new byte[1024];
DatagramPacket ret = new DatagramPacket(byteret,byteret.length);
ds.receive(ret);
byte[] dateret = ret.getData();
int lenret = ret.getLength();
//输出数据
String rets = new String(dateret,0,lenret);
System.out.println(rets);
if(rets.equals("登录成功!"))
break;
}
//
//发送查询关键字
Thread.sleep(1000);
System.out.println("\n请输入查询关键字——ID、姓名、手机号!");
String key = inputs.nextLine();
byte[] bytekey = key.getBytes();
int lenkey = key.length();
DatagramPacket dkey = new DatagramPacket(bytekey,lenkey,address,port);
ds.send(dkey);
//接收服务器端返回的信息!
Thread.sleep(1000);
//响应1000ms
byte[] bytemage = new byte[1024];
DatagramPacket massage = new DatagramPacket(bytemage,bytemage.length);
ds.receive(massage);
//byte[] getData() :获取接收到的数据,也可以直接使用创建包对象时的数组
byte[] datemage = massage.getData();
//int getLength() :获取具体收到数据的长度
int lenmage = massage.getLength();
//输出数据
System.out.println(" ID | 密码 | 姓名 | 电话 | 单位地址 | 单位电话 | 家庭电话 |");
System.out.println(new String(datemage,0,lenmage));
ds.close();
}
}
二、服务器端代码
package 服务器程序;
import java.beans.Statement;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import 数据库连接.DBUtil;
public class Server {
public static void main(String[] args) throws IOException, SQLException {
//创建数据库连接对象
DBUtil link = new DBUtil();
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(8888);
//DatagramSocket ds = new DatagramSocket(8888);
byte[] bysid = new byte[1024];
byte[] byspw = new byte[1024];
DatagramPacket did = new DatagramPacket(bysid,bysid.length);
DatagramPacket dpw = new DatagramPacket(byspw,byspw.length);
InetAddress address;
int port = 6666;
//System.out.println(1);
while(true)
{
ds.receive(did);//阻塞
ds.receive(dpw);
//wSystem.out.println(2);
//解析数据
//InetAddress getAddress() : 获取发送端的IP对象
address = did.getAddress();
//byte[] getData() :获取接收到的数据,也可以直接使用创建包对象时的数组
byte[] dateid = did.getData();
byte[] datepw = dpw.getData();
//int getLength() :获取具体收到数据的长度
int lenid = did.getLength();
int lenpw = dpw.getLength();
String id = new String(dateid,0,lenid);
String pw = new String(datepw,0,lenpw);
//输出数据
// System.out.println("sender ---> " + address.getHostAddress());
// System.out.println(new String(dateid,0,lenid));
// System.out.println(new String(datepw,0,lenpw));
//
//发送客服端登录信息!
String ret = "登录失败!";
java.sql.Statement stmt = link.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("select ID,PW from 何志行");
while(rs.next())
{
if(id.equals(rs.getString("ID"))&&pw.equals(rs.getString("PW")))
ret = "登录成功!";
//验证ID和PW是否正确
}
byte[] byteret = ret.getBytes();
int lenret = byteret.length;
DatagramPacket dret = new DatagramPacket(byteret,lenret,address,port);
ds.send(dret);
if(ret.equals("登录成功!"))
break;
}
//
//接收客户端查询信息关键字!
byte[] bytekey = new byte[1024];
DatagramPacket dkey = new DatagramPacket(bytekey,bytekey.length);
ds.receive(dkey);
byte[] datekey = dkey.getData();
int lenkey = dkey.getLength();
String key = new String(datekey,0,lenkey);
java.sql.Statement stmt = link.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("select ID,PW,name,ptel,uaddress,utel,htel from 何志行");
String massage = "没有用户记录!";
//对比key与数据库内(ID、姓名、手机号)
while(rs.next())
{
if(key.equals(rs.getString("ID")) || key.equals(rs.getString("name")) || key.equals(rs.getString("ptel")))
//发送关于关键字的相关信息!
massage =rs.getString("ID")+" | "+rs.getString("PW")+" | "+rs.getString("name")+" |"+rs.getString("ptel")+"| "+rs.getString("uaddress")+"| "+rs.getString("utel")+" | "+rs.getString("htel")+" |";
}
/
//发送关于关键字的相关信息!
// String massage =(rs.getString("ID")+" | "+rs.getString("PW")+" | "+rs.getString("name")+"|"+rs.getString("ptel")+"| "+rs.getString("uaddress")+"| "+rs.getString("utel")+" | "+rs.getString("htel")+" |");
byte[] bytemage = massage.getBytes();
int lenmage = bytemage.length;
DatagramPacket dmage = new DatagramPacket(bytemage,lenmage,address,port);
ds.send(dmage);
//释放资源
ds.close();
}
}
三、连接数据库代码
package 数据库连接;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class DBUtil {
//这里可以设置数据库名称
private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=何志行";
private static final String USER="sa";
private static final String PASSWORD="123456";
private static Connection conn=null;
//静态代码块(将加载驱动、连接数据库放入静态块中)
static{
try {
//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载驱动成功");
//2.获得数据库的连接
conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("数据库连接成功");
}
//对外提供一个方法来获取数据库连接
public static Connection getConnection(){
return conn;
}
//测试用例
public static void main(String[] args) throws Exception{
java.sql.Statement stmt = conn.createStatement();
//ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 ,返回一个结果集(ResultSet)对象。
ResultSet rs = ((java.sql.Statement) stmt).executeQuery("select ID,PW,name,ptel,uaddress,utel,htel from 何志行");
System.out.println(" ID | 密码 | 姓名 | 电话 | 单位地址 | 单位电话 | 家庭电话 |");
while(rs.next()){//如果对象中有数据,就会循环打印出来
System.out.println(rs.getString("ID")+" | "+rs.getString("PW")+" | "+rs.getString("name")+" |"+rs.getString("ptel")+"| "+rs.getString("uaddress")+"| "+rs.getString("utel")+" | "+rs.getString("htel")+" |");
// Scanner inputs = new Scanner(System.in);
// System.out.println("请输入ID:");
// String IDD = inputs.nextLine();
// String s = "何志行",s1 = "123456";
// if(s.equals(rs.getString("ID")) && s1.equals(rs.getString("PW")))
// {
// System.out.println("登陆成功");
// }
// else
// {
// System.out.println("登录失败");
// }
}
}
}
//import java.sql.Connection;
//import java.sql.Driver;
//import java.sql.DriverManager;
//import java.sql.Statement;
//
///**
// * @author 李苏亚
// *lacfk;as
// *;v,;s
// */
//public class JDBC {
//
// /**
// * @param args
// */
// public static void main(String[] args) {
// // TODO 自动生成的方法存根
//
//
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerClob());
// String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
// String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=UserAndPassword";
// String userName="sa";
// String userPwd="lsyiua5211314.";
// try
// {
// Class.forName(driverName);
// System.out.println("加载驱动成功");
// }catch(Exception e){
// e.printStackTrace();
// System.out.println("加载驱动失败");
// }
// try{
// Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
// System.out.println("连接数据库成功");
// }catch(Exception e)
// {
// e.printStackTrace();
// System.out.print("SQL Server连接失败");
// }
// //连接数据查询数据库内容
//
//
// }
//
//}
四、数据库连接的工具JDBC
需要自己百度查找jdbc数据库连接工具,配置运行后,方能启用数据库连接代码。才会连接成功。