This commit is contained in:
xixu-me committed 2024-07-02 15:17:40 +08:00
1 parent 8d25963b92
commit 9a5f8c7a90
12 files changed
+230 -268

No files matched your search

+2 -2
View File
@@ -79,10 +79,10 @@ public class Client {
update();
break;
case 6:
ExportToCSV.to();
ExportToCSV.to("output.csv", "select * from book");
break;
case 7:
ExportToXLS.to();
ExportToXLS.to("select * from book", "output.xls");
break;
default:
break;
+12 -8
View File
@@ -6,14 +6,18 @@ import java.net.Socket;
import server.thread.HandleThread;
public class Server {
public static void main(String[] args) throws Exception {
int port = 9999;
ServerSocket server = new ServerSocket(port);
System.out.println("The server is listening for client connection requests on port " + port + ".");
while (true) {
Socket socket = server.accept();
System.out.println("Client " + socket.getInetAddress().getHostAddress() + " successfully connected!");
new HandleThread(socket).start();
private static final int PORT = 9999;
public static void main(String[] args) {
try (ServerSocket server = new ServerSocket(PORT)) {
System.out.println("Server listening on port " + PORT);
while (true) {
Socket clientSocket = server.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
new Thread(new HandleThread(clientSocket)).start();
}
} catch (Exception e) {
System.err.println("Server exception: " + e.getMessage());
}
}
}
+25 -27
View File
@@ -10,38 +10,36 @@ import server.vo.User;
public class UserDAO {
public static User get(String userName) throws Exception {
User user = null;
Connection con = DBConnection.getConnection();
String sql = "select * from user where userName=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, userName);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
user = new User();
user.setUserName(rs.getString("userName"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setRole(rs.getString("role"));
String sql = "SELECT * FROM user WHERE userName=?";
try (Connection con = DBConnection.getConnection();
PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, userName);
try (ResultSet rs = pst.executeQuery()) {
if (rs.next())
return createUserFromResultSet(rs);
}
}
DBConnection.close(con, pst);
return user;
return null;
}
public static ArrayList<User> query(User userCondition) throws Exception {
ArrayList<User> list = new ArrayList<User>();
Connection con = DBConnection.getConnection();
String sql = "*******";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
User user = new User();
user.setUserName(rs.getString("userName"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setRole(rs.getString("role"));
list.add(user);
ArrayList<User> list = new ArrayList<>();
String sql = "SELECT * FROM user"; // 示例SQL,实际应根据userCondition调整
try (Connection con = DBConnection.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery()) {
while (rs.next())
list.add(createUserFromResultSet(rs));
}
DBConnection.close(con, pst);
return list;
}
private static User createUserFromResultSet(ResultSet rs) throws Exception {
User user = new User();
user.setUserName(rs.getString("userName"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setRole(rs.getString("role"));
return user;
}
}
+44 -63
View File
@@ -1,9 +1,7 @@
package server.thread;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
@@ -11,98 +9,81 @@ import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import server.dao.UserDAO;
import server.vo.User;
public class HandleThread extends Thread {
private Socket socket;
private BufferedReader buf;
private PrintWriter write;
public HandleThread(Socket socket) {
super();
this.socket = socket;
try {
InputStream is = socket.getInputStream();
buf = new BufferedReader(new InputStreamReader(is, "utf-8"));
OutputStream os = socket.getOutputStream();
write = new PrintWriter(new OutputStreamWriter(os, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String receiveStr = buf.readLine();
try (BufferedReader buf = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
PrintWriter write = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"))) {
String receiveStr;
while ((receiveStr = buf.readLine()) != null) {
System.out.println("Receive the client's request data: " + receiveStr);
JsonElement element = JsonParser.parseString(receiveStr);
JsonObject obj = element.getAsJsonObject();
JsonObject obj = new Gson().fromJson(receiveStr, JsonObject.class);
int code = obj.get("code").getAsInt();
if (code == 8) {
close();
if (code == 8)
break;
}
String data = obj.get("data").getAsString();
String response = "";
switch (code) {
case 0:
response = loginHandle(data);
break;
case 1:
break;
case 2:
break;
default:
System.out.println("Invalid command!");
break;
}
String response = handleRequest(code, data);
write.println(response);
write.flush();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Client exited!");
closeResources();
}
System.out.println("Client exited!");
}
public void close() throws Exception {
if (write != null)
write.close();
if (buf != null)
buf.close();
if (socket != null)
socket.close();
private String handleRequest(int code, String data) throws Exception {
switch (code) {
case 0:
return loginHandle(data);
case 1:
// Handle other cases
return "";
case 2:
// Handle other cases
return "";
default:
System.out.println("Invalid command!");
return "";
}
}
public String loginHandle(String str) throws Exception {
private String loginHandle(String str) throws Exception {
User loginUser = new Gson().fromJson(str, User.class);
User user = UserDAO.get(loginUser.getUserName());
String responseData = "";
Map<String, Object> map = new HashMap<>();
if (user == null) {
Map<String, Object> map = new HashMap<>();
map.put("code", 0);
map.put("data", "Username does not exist!");
responseData = new Gson().toJson(map);
} else if (loginUser.getPassword().equals(user.getPassword())) {
map.put("code", 1);
map.put("data", new Gson().toJson(user));
} else {
if (loginUser.getPassword().equals(user.getPassword())) {
Map<String, Object> map = new HashMap<>();
map.put("code", 1);
map.put("data", new Gson().toJson(user));
responseData = new Gson().toJson(map);
} else {
Map<String, Object> map = new HashMap<>();
map.put("code", 0);
map.put("data", "Wrong password!");
responseData = new Gson().toJson(map);
}
map.put("code", 0);
map.put("data", "Wrong password!");
}
return responseData;
return new Gson().toJson(map);
}
}
private void closeResources() {
try {
if (socket != null)
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
+14 -15
View File
@@ -2,7 +2,6 @@ package server.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBConnection {
@@ -10,32 +9,32 @@ public class DBConnection {
private final static String url = "jdbc:mysql://localhost:3306/lda?allowPublicKeyRetrieval=true&useSSL=false";
private final static String user = "root";
private final static String password = "516849";
static {
try {
Class.forName(driverName);
} catch (Exception e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failed to get database connection", e);
}
return connection;
}
public static void close(Connection con, PreparedStatement pst) {
try {
if (pst != null)
pst.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
public static void close(AutoCloseable... resources) {
for (AutoCloseable resource : resources) {
if (resource != null) {
try {
resource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
+26 -19
View File
@@ -8,29 +8,36 @@ import java.sql.SQLException;
import java.sql.Statement;
public class ExportToCSV {
public static void to() {
String outputFile = "Library Data.csv";
public static void to(String outputFile, String query) {
try (Connection connection = DBConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM book");
ResultSet resultSet = statement.executeQuery(query);
FileWriter fileWriter = new FileWriter(outputFile)) {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
fileWriter.append(resultSet.getMetaData().getColumnName(i));
if (i < resultSet.getMetaData().getColumnCount())
fileWriter.append(",");
}
fileWriter.append("\n");
while (resultSet.next()) {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
fileWriter.append(resultSet.getString(i));
if (i < resultSet.getMetaData().getColumnCount())
fileWriter.append(",");
}
fileWriter.append("\n");
}
System.out.println("Data exported successfully!");
writeColumnNames(resultSet, fileWriter);
writeDataRows(resultSet, fileWriter);
System.out.println("Data exported successfully to " + outputFile);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
}
private static void writeColumnNames(ResultSet resultSet, FileWriter fileWriter) throws SQLException, IOException {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
fileWriter.append(resultSet.getMetaData().getColumnName(i));
if (i < resultSet.getMetaData().getColumnCount())
fileWriter.append(",");
}
fileWriter.append("\n");
}
private static void writeDataRows(ResultSet resultSet, FileWriter fileWriter) throws SQLException, IOException {
while (resultSet.next()) {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
fileWriter.append(resultSet.getString(i));
if (i < resultSet.getMetaData().getColumnCount())
fileWriter.append(",");
}
fileWriter.append("\n");
}
}
}
+28 -27
View File
@@ -11,37 +11,38 @@ import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportToXLS {
public static void to() {
public static void to(String query, String outputPath) {
String[] columnHeaders = { "title", "author", "publisher", "oldprice", "newprice", "href" };
try (Connection connection = DBConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM book");) {
WritableWorkbook book = Workbook.createWorkbook(new File("Library Data.xls"));
WritableSheet sheet = book.createSheet("sheet1", 0);
Label label = new Label(0, 0, "title");
sheet.addCell(label);
label = new Label(1, 0, "author");
sheet.addCell(label);
label = new Label(2, 0, "publisher");
sheet.addCell(label);
label = new Label(3, 0, "oldprice");
sheet.addCell(label);
label = new Label(4, 0, "newprice");
sheet.addCell(label);
label = new Label(5, 0, "href");
sheet.addCell(label);
int i = 1;
while (rs.next()) {
for (int j = 0; j <= 5; j++) {
label = new Label(j, i, rs.getString(j + 1));
sheet.addCell(label);
}
i++;
}
book.write();
book.close();
System.out.println("Data exported successfully!");
ResultSet rs = statement.executeQuery(query)) {
WritableWorkbook workbook = Workbook.createWorkbook(new File(outputPath));
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
createColumnHeaders(sheet, columnHeaders);
writeDataRows(sheet, rs);
workbook.write();
workbook.close();
System.out.println("Data exported successfully to " + outputPath);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void createColumnHeaders(WritableSheet sheet, String[] columnHeaders) throws Exception {
for (int i = 0; i < columnHeaders.length; i++) {
Label label = new Label(i, 0, columnHeaders[i]);
sheet.addCell(label);
}
}
private static void writeDataRows(WritableSheet sheet, ResultSet rs) throws Exception {
int rowIndex = 1;
while (rs.next()) {
for (int columnIndex = 0; columnIndex <= 5; columnIndex++) {
Label label = new Label(columnIndex, rowIndex, rs.getString(columnIndex + 1));
sheet.addCell(label);
}
rowIndex++;
}
}
}
+2 -1
View File
@@ -69,6 +69,7 @@ public class Book {
}
public String toString() {
return "Title: " + this.title + ", Author: " + this.author + ", Publisher: " + this.publisher + ", Original Price: " + this.oldprice + ", Discounted Price: " + this.newprice + ", URL: " + this.href;
return "Title: " + this.title + ", Author: " + this.author + ", Publisher: " + this.publisher
+ ", Original Price: " + this.oldprice + ", Discounted Price: " + this.newprice + ", URL: " + this.href;
}
}
+16 -42
View File
@@ -2,9 +2,7 @@ package server.tools.crawling;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@@ -12,43 +10,22 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CrawlerTools {
public static String get(String urlStr, String charset) {
StringBuffer buf = new StringBuffer();
HttpURLConnection con = null;
InputStream in = null;
BufferedReader read = null;
try {
URL url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53");
int code = con.getResponseCode();
if (code == 200) {
in = con.getInputStream();
read = new BufferedReader(new InputStreamReader(in, charset));
String info = "";
public static String get(String urlStr, String charset) throws IOException {
StringBuilder buf = new StringBuilder();
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53");
if (con.getResponseCode() == 200) {
try (BufferedReader read = new BufferedReader(new InputStreamReader(con.getInputStream(), charset))) {
String info;
while ((info = read.readLine()) != null)
buf.append(info);
} else
System.out.println("Error! Response code: " + code + ".");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (read != null)
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (con != null)
con.disconnect();
}
} else {
System.out.println("Error! Response code: " + con.getResponseCode() + ".");
}
con.disconnect();
return buf.toString();
}
@@ -58,15 +35,12 @@ public class CrawlerTools {
Matcher mat = pat.matcher(url);
while (mat.find()) {
String hanzi = mat.group();
System.out.println(hanzi);
String encodehanzi = "";
try {
encodehanzi = URLEncoder.encode(hanzi, "utf-8");
} catch (UnsupportedEncodingException e) {
url = url.replace(hanzi, URLEncoder.encode(hanzi, "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
url = url.replaceAll(hanzi, encodehanzi);
}
return url;
}
}
}
+23 -10
View File
@@ -1,19 +1,32 @@
package server.tools.crawling;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Driver {
private static final String URL_PREFIX = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-";
public static void crawl() {
ExecutorService executor = Executors.newFixedThreadPool(5);
try {
String url = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1";
Thread thread1 = new Thread(new NewsThread(url));
thread1.start();
for (int i = 2; i <= 5; i++) {
String url2 = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-" + i;
Thread thread2 = new Thread(new NewsThread(url2));
thread2.start();
Thread.sleep(10000);
for (int i = 1; i <= 5; i++) {
String url = URL_PREFIX + i;
executor.submit(new NewsThread(url));
if (i > 1)
TimeUnit.SECONDS.sleep(10);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Thread was interrupted, Failed to complete operation");
} finally {
executor.shutdown();
try {
if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS))
executor.shutdownNow();
} catch (InterruptedException e) {
executor.shutdownNow();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
+38 -49
View File
@@ -1,7 +1,9 @@
package server.tools.crawling;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
@@ -14,63 +16,50 @@ public class NewsThread implements Runnable {
private String urlPath;
public NewsThread(String urlPath) {
super();
this.urlPath = urlPath;
}
@Override
public void run() {
String content = CrawlerTools.get(urlPath, "GB2312");
Document doc = Jsoup.parse(content);
Elements elements = doc.select(".bang_wrapper .bang_list_box ul li");
Connection con = DBConnection.getConnection();
PreparedStatement ps = null;
String sql = null;
int index = 0;
for (Element bookelement : elements) {
index++;
if (index >= 20)
break;
String title = bookelement.select(".name a").text();
Elements publisherInfoElements = bookelement.select(".publisher_info");
String author = null;
String publisher = null;
if (!publisherInfoElements.isEmpty()) {
Element firstPublisherInfoElement = publisherInfoElements.get(0);
Element secondPublisherInfoElement = publisherInfoElements.get(1);
author = firstPublisherInfoElement.select("a").text();
publisher = secondPublisherInfoElement.select("a").text();
}
String oprice = bookelement.select(".price_r").text();
String nprice = bookelement.select(".price_n").first().text();
String href = bookelement.select(".name a").attr("href");
double oldprice = Double.parseDouble(oprice.substring(1));
double newprice = Double.parseDouble(nprice.substring(1));
sql = "insert into book values(?,?,?,?,?,?)";
try {
ps = con.prepareStatement(sql);
ps.setString(1, title);
ps.setString(2, author);
ps.setString(3, publisher);
ps.setDouble(4, oldprice);
ps.setDouble(5, newprice);
ps.setString(6, href);
ps.executeUpdate();
} catch (Exception e) {
try {
String content = CrawlerTools.get(urlPath, "GB2312");
Document doc = Jsoup.parse(content);
Elements elements = doc.select(".bang_wrapper .bang_list_box ul li");
try (Connection con = DBConnection.getConnection()) {
for (Element bookElement : elements.subList(0, Math.min(elements.size(), 20))) {
String title = bookElement.select(".name a").text();
Elements publisherInfoElements = bookElement.select(".publisher_info");
String author = publisherInfoElements.size() > 0 ? publisherInfoElements.get(0).select("a").text()
: null;
String publisher = publisherInfoElements.size() > 1
? publisherInfoElements.get(1).select("a").text()
: null;
String oprice = bookElement.select(".price_r").text();
String nprice = bookElement.select(".price_n").first().text();
String href = bookElement.select(".name a").attr("href");
double oldprice = Double.parseDouble(oprice.substring(1));
double newprice = Double.parseDouble(nprice.substring(1));
insertBook(con, title, author, publisher, oldprice, newprice, href);
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(title);
System.out.println(author);
System.out.println(publisher);
System.out.println(oldprice);
System.out.println(newprice);
System.out.println(href);
}
try {
ps.close();
con.close();
} catch (Exception e) {
} catch (IOException e) {
e.printStackTrace();
}
}
private void insertBook(Connection con, String title, String author, String publisher, double oldprice,
double newprice, String href) throws SQLException {
String sql = "INSERT INTO book VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, title);
ps.setString(2, author);
ps.setString(3, publisher);
ps.setDouble(4, oldprice);
ps.setDouble(5, newprice);
ps.setString(6, href);
ps.executeUpdate();
}
}
}
-5
View File
@@ -45,9 +45,4 @@ public class User {
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return userName + "\t" + password + "\t" + name + "\t" + role;
}
}