📄 db2rs.java
字号:
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.sql.*;
public class DB2RS {
private int port = 4004;
private ServerSocket serverSocket;
static final String dbURL =
"jdbc:mysql://localhost/j2mebook?" +
"user=j2meapps&password=bigsecret";
private static final int ID_DOWNLOAD = 0;
private static final int ID_SYNCHRONIZE = 2;
private static final int ID_EXIT = 3;
public DB2RS() throws ClassNotFoundException {
Class.forName("org.gjt.mm.mysql.Driver");
}
public void acceptConnections() {
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.err.println("ServerSocket instantiation failure");
e.printStackTrace();
System.exit(0);
}
while (true) {
try {
Socket newConnection = serverSocket.accept();
ServerThread st = new ServerThread(newConnection);
new Thread(st).start();
}
catch (IOException ioe) {
System.err.println("server accept failed");
}
}
}
public static void main(String args[]) {
DB2RS server = null;
try {
server = new DB2RS();
}
catch (ClassNotFoundException e) {
System.out.println("unable to load JDBC driver");
e.printStackTrace();
System.exit(1);
}
server.acceptConnections();
}
class ServerThread implements Runnable {
private Socket socket;
private BufferedReader datain;
private BufferedWriter dataout;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
String line = null;
try {
datain = new BufferedReader(new InputStreamReader
(socket.getInputStream()));
dataout = new BufferedWriter(new OutputStreamWriter
(socket.getOutputStream()));
}
catch (IOException e) {
return;
}
byte[] ba = new byte[1024];
int op = -1;
try {
line = datain.readLine();
op = Integer.parseInt(line);
switch (op) {
case ID_DOWNLOAD:
String division = datain.readLine();
download(dataout, division);
break;
case ID_SYNCHRONIZE:
synchronize(datain);
break;
default:
break;
}
socket.close();
}
catch (IOException ioe) {
}
}
private void synchronize(BufferedReader br)
throws IOException {
Connection dbConn = null;
try {
dbConn = DriverManager.getConnection(dbURL);
String update = "UPDATE fundraiser SET amount = " +
" ? WHERE division = ? AND " +
" branch = ?";
PreparedStatement stmt =
dbConn.prepareStatement(update);
while (true) {
String line = br.readLine();
if (line.length() == 0) {
break;
}
int ix = line.indexOf("\t");
String division = line.substring(0,ix);
line = line.substring(ix+1);
ix = line.indexOf("\t");
String branch = line.substring(0,ix);
int amount = Integer.parseInt(line.substring(ix+1));
stmt.setInt(1,amount);
stmt.setString(2,division);
stmt.setString(3,branch);
stmt.executeUpdate();
}
}
catch (SQLException e) {
System.out.println(e);
}
finally {
if (dbConn != null) {
try {
dbConn.close();
}
catch (SQLException e) {
}
}
}
}
private void download(BufferedWriter bw, String division)
throws IOException {
Connection dbConn = null;
try {
dbConn = DriverManager.getConnection(dbURL);
Statement stmt = dbConn.createStatement();
String query = "SELECT * " +
"FROM fundraiser " + "WHERE division = " +
"'" + division + "'";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
StringBuffer sb = new StringBuffer();
sb.append(rs.getString(1));
sb.append("\t");
sb.append(rs.getString(2));
sb.append("\t");
sb.append(rs.getString(3));
sb.append('\n');
dataout.write(sb.toString());
}
dataout.write("\n");
dataout.flush();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
if (dbConn != null) {
try {
dbConn.close();
}
catch (SQLException e) {
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -