今天看见论坛有人在问远程操作数据库的贴子,正好以前我也写过这么一个简陋的程序能简单的实现这个功能。
先附上这个程序的流程
Index程序入口模块
代码:
importjava.util.Scanner;
publicclassIndex {
publicstaticvoidmain(String[] args){
Scanner sc;
booleana =true;
while(a){
sc =newScanner(System.in);
System.out.println("请输入需要执行的任务:1.查询数据 2.插入数据 3.开始接收数据 4.删除数据 0.退出");
intchoose = sc.nextInt();
if(choose == 1){
newConSql().select();
}
if(choose == 2){
sc =newScanner(System.in);
String str = sc.nextLine();
newConSql().insert(str);
}
if(choose == 3){
newServer().start();
}
if(choose == 4){
newConSql().delete();
}
if(choose == 0){
System.out.print("退出!");
break;
}
}
}
}
这部分简单的代码实现选择操作。 (插入代码这个也不好看。。。就直接发吧)。。
下面是第二部分,数据库操作模块
importjava.sql.*;
publicclassConSql {
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
privatestaticintid;
voidinsert(String x) {
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost/wc?user=root&password=root");
stmt=conn.createStatement();
String sql ="insert into dept values('"+id++ +"','"+ x +"')";
System.out.print("gei the string!\n");
stmt.execute(sql);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException ex) {
System.out.println("SQLException: "+ ex.getMessage());
System.out.println("SQLState: "+ ex.getSQLState());
System.out.println("VendorError: "+ ex.getErrorCode());
}finally{
try{
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
if(conn!=null) {
conn.close();
conn=null;
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
voidselect() {
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost/wc?user=root&password=root");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from dept;");
while(rs.next()){System.out.println(rs.getString("id")+"\t"+rs.getString("number"));
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException ex) {
System.out.println("SQLException: "+ ex.getMessage());
System.out.println("SQLState: "+ ex.getSQLState());
System.out.println("VendorError: "+ ex.getErrorCode());
}finally{
try{
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
if(conn!=null) {
conn.close();
conn=null;
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
voiddelete() {
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager
.getConnection("jdbc:mysql://localhost/wc?user=root&password=root");
stmt=conn.createStatement();
String sql ="delete from dept;";
System.out.print("ok!\n");
stmt.execute(sql);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException ex) {
System.out.println("SQLException: "+ ex.getMessage());
System.out.println("SQLState: "+ ex.getSQLState());
System.out.println("VendorError: "+ ex.getErrorCode());
}finally{
try{
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
if(conn!=null) {
conn.close();
conn=null;
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
数据库部分
主要实现增删查功能。
下面是第三部分,服务器代码
Server服务器模块
代码:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
boolean started = false;
ServerSocket ss = null;
public void start() {
System.out.print("服务器已开启,开始接收数据!");
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
System.out.println("a client connected!");
new Thread(new Client(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable{
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s){
this.s = s;
try{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
}catch(IOException e){
e.printStackTrace();
}
}
public void run() {
try{
while(bConnected) {
String str = dis.readUTF();
System.out.print("接收到"+str+"\t\t");
new ConSql().insert(str);
}
}catch(EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
服务器部分,只要实现对端口的监听,进行接受数据库发来的数据,并插入数据库中
下面是第四部分,客户端
Clients客户端模块
基本客户端举例
代码://这段代码只具有基本的跟据用户输入内容进行插入的功能
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Clients extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new Clients().launchFrame();
}
}
好久之前写的了,可能有的地方太简陋。。
希望大家一起交流交流。。。。