今天看见论坛有人在问远程操作数据库的贴子,正好以前我也写过这么一个简陋的程序能简单的实现这个功能。
先附上这个程序的流程
Index程序入口模块
代码:
import java.util.Scanner;
public class Index {
public static void main(String[] args){
Scanner sc;
boolean a = true;
while(a){
sc = new Scanner(System.in);
System.out.println("请输入需要执行的任务:1.查询数据 2.插入数据 3.开始接收数据 4.删除数据 0.退出");
int choose = sc.nextInt();
if(choose == 1){
new ConSql().select();
}
if(choose == 2){
sc = new Scanner(System.in);
String str = sc.nextLine();
new ConSql().insert(str);
}
if(choose == 3){
new Server().start();
}
if(choose == 4){
new ConSql().delete();
}
if(choose == 0){
System.out.print("退出!");
break;
}
}
}
}
这部分简单的代码实现选择操作。
(插入代码这个也不好看。。。就直接发吧)。。
下面是第二部分,数据库操作模块
import java.sql.*;
public class ConSql {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
private static int id ;
void insert(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++ +