Java Server MySQL Update不起作用

时间:2015-10-14 23:25:31

标签: java mysql

我最近在java中启动了一个大型服务器应用程序。 问题是,我的用户更新功能usr.updUser(...)无效。

首先是文件。

Server.java:

import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class Server extends Thread {

    private ServerSocket srvSock;
    SQLdb db;
    User usr;

    public Server(int port, SQLdb db) throws IOException{
        srvSock = new ServerSocket(port);
        this.db = db;
    }

    public void run(){
        while(true){
            String ret = "";

            try{
                Socket sock = srvSock.accept();

                usr = new User(db);

                DataInputStream in = new DataInputStream(sock.getInputStream());
                try{
                    String[] cmd = in.readUTF().split(":");
                    switch(cmd[0]){
                    case "user":{
                        switch(cmd[1]){
                        case "addUser":{
                            ret = usr.addUser(cmd[2]);
                            break;
                        }
                        case "getUser":{
                            ret = usr.getUser(cmd[2]);
                            break;
                        }
                        case "updUser":{
                            ret = usr.updUser(cmd[2]);
                            break;
                        }
                        case "delUser":{
                            ret = usr.delUser(cmd[2]);
                            break;
                        }
                        }
                        break;
                    }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }

                DataOutputStream out = new DataOutputStream(sock.getOutputStream());
                out.writeUTF(ret);

                sock.close();
            }catch(IOException e){
                e.printStackTrace();
                break;
            }
        }
    }

    public static void main(String[] args){
        SQLdb db = new SQLdb("data.db");

        User usr = new User(db);
        if(args[0] == "new")
            usr.setupTable();

        usr.updUser("age=6;uid=2"));

        try{
            Thread server = new Server(5000, db);
            server.start();
        }catch(IOException e){
            e.printStackTrace();
        }

        Thread ti = new TermInput(db);
        ti.start();

        Thread time = new Time(usr);
        time.start();
    }
}

User.java:

import java.sql.SQLException;

public class User {

    SQLdb db;
    String report;

    public User(SQLdb db){
        this.db = db;
    }

    public String setupTable(){
        if(
           db.addTable("users",
                       "uid    INTEGER     PRIMARY KEY AUTOINCREMENT   NOT NULL, " +
                       "uname  TEXT        UNIQUE                      NOT NULL, " +
                       "pwd    TEXT                                    NOT NULL, " +
                       "mail   TEXT        UNIQUE                      NOT NULL, " +
                       "age    INTEGER                                 NOT NULL, " +
                       "rank   INTEGER                 DEFAULT         0       , " +
                       "coins  INTEGER                 DEFAULT         100     , " +
                       "prem   INT                     DEFAULT         0       , " +
                       "setg   TEXT")
           )
            report = "ok";
        else
            report = db.report;
        return report;
    }

    public String addUser(String cmd){
        try{
            String[] _cmd = cmd.split(";");
            if(db.addEntry("users", new String[] {"uname, pwd, mail, age",
                                                  "'" + _cmd[0] + "', '" + _cmd[1] + "', '" + _cmd[2] + "', " + _cmd[3]}))
                report = "ok";
            else
                report = db.report;
        }catch(Exception e){
            report = e.getMessage();
        }
        return report;
    }

    public String getUser(String cmd){
        try{
            String[] _cmd = cmd.split(";");
            if(db.getEntry("users", new String[] {_cmd[0], _cmd[1]})){
                try{
                    report = "";
                    while(db.res.next()){
                        report += Integer.toString(db.res.getInt("uid")) + ";";
                        report += db.res.getString("uname") + ";";
                        report += db.res.getString("pwd") + ";";
                        report += db.res.getString("mail") + ";";
                        report += Integer.toString(db.res.getInt("age")) + ";";
                        report += Integer.toString(db.res.getInt("rank")) + ";";
                        report += Integer.toString(db.res.getInt("coins")) + ";";
                        report += Integer.toString(db.res.getInt("prem")) + ";";
                        report += db.res.getString("setg");
                        report += "-";
                    }
                    db.res.close();
                    db.stmt.close();
                }catch(SQLException e){
                    report = e.getMessage();
                }
            }
        }catch(Exception e){
            report = e.getMessage();
        }
        return report;
    }

    public String updUser(String cmd){
        try{
            String[] _cmd = cmd.split(";");
            if(db.updEntry("users", new String[] {_cmd[0], _cmd[1]}))
                report = "ok";
            else
                report = db.report;
        }catch(Exception e){
            report = e.getMessage();
        }
        return report;
    }

    public String delUser(String cmd){
        try{
            if(db.delEntry("users", cmd))
                report = "ok";
            else
                report = db.report;
        }catch(Exception e){
            report = e.getMessage();
        }
        return report;
    }
}

SQLdb.java:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SQLdb {

    Connection conn;
    Statement stmt;
    String report;
    ResultSet res;

    public SQLdb(String db){
        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + db);
            report = "";
        }catch(Exception e){
            report = e.getMessage();
        }
    }

    public boolean addTable(String table, String struct){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("CREATE TABLE " + table + " (" + struct + ");");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean resTable(String table){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM " + table + ";");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean delTable(String table){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("DROP TABLE " + table + ";");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean addEntry(String table, String[] entry){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO " + table + "(" + entry[0] + ") VALUES (" + entry[1] + ");");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean getEntry(String table, String[] ident){
        try{
            stmt = conn.createStatement();
            String exec = "SELECT " + ident[0] + " FROM " + table;
            if(ident[1].equals("-")){
                exec += ";";
            }else{
                exec += " WHERE (" + ident[1] + ");";
            }
            res = stmt.executeQuery(exec);
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean updEntry(String table, String[] data){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("UPDATE " + table + " set " + data[1] + " WHERE " + data[0] + ";");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }

    public boolean delEntry(String table, String entry){
        try{
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM " + table + " WHERE (" + entry + ");");
            stmt.close();
            report = "";
            return true;
        }catch(SQLException e){
            report = e.getMessage();
            return false;
        }
    }
}

TermInput.java:

import java.util.Scanner;

import java.sql.SQLException;

public class TermInput extends Thread {

    Scanner s;
    SQLdb sql;

    public TermInput(SQLdb sql){
        s = new Scanner(System.in);
        this.sql = sql;
    }

    public void run(){
        System.out.println("\nUse 'help' to get a list of available commands!\n");
        boolean exit = false;
        while(true){
            System.out.print(">> ");
            switch(s.next()){
            case "help":
            case "h":
            case "?":{
                System.out.println(
                                   "   [h]elp/[?]\tList all commands\n" +
                                   "   [s]hutdown\tShutdown the server"
                                   );
                break;
            }
            case "shutdown":
            case "s":{
                exit = true;
                break;
            }
            case "time":{
                System.out.println(Time.currentTime());
                break;
            }
            default:{
                System.out.println(
                                   "   Unknown command!\n" +
                                   "   Use 'help' to get a list of available commands!"
                                   );
                break;
            }
            }
            if(exit) break;
        }

        try{
            this.sql.conn.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
        System.exit(0);
    }
}

Time.java:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.SQLException;

public class Time extends Thread{

    User usr;

    public Time(User usr){
        this.usr = usr;
    }

    public void run(){
        while(true){
            checkTime();
        }
    }

    private void checkTime(){
        String ret = "";
        ret = usr.getUser("*;-");
        String[] prem = ret.split("-");
        for(int x = 0; x < prem.length; x++)
            System.out.println(prem[x].split(";")[7]);
    }

    public static int currentTime() {
        int d = 0;
        SimpleDateFormat s;
        Calendar c = Calendar.getInstance();
        s = new SimpleDateFormat("DDD");
        d = (Integer.parseInt(s.format(c.getTime()))-1)*24*60*60;
        s = new SimpleDateFormat("HH");
        d += (Integer.parseInt(s.format(c.getTime()))-1)*60*60;
        s = new SimpleDateFormat("mm");
        d += (Integer.parseInt(s.format(c.getTime()))-1)*60;
        s = new SimpleDateFormat("ss");
        d += Integer.parseInt(s.format(c.getTime()));
        return d;
    }
}

您可以在Server.java的第76行看到我想要更改ID为2到6年的用户年龄。 但是当我发出getUser请求时,它没有改变(参见我的无限通话函数)。

那么为什么用户没有通过我的功能更新?

0 个答案:

没有答案