您好我正在努力让我的数据库从连接到服务器的客户端实例接收用户名和IP地址。
这是一个竞价系统,最初只允许客户的多个单独实例对2个项目出价,现在我需要添加名称(用户提示在加载客户端实例时输入)和他们的IP地址到我的数据库。
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null; //null pointer exception fix
Statement statement = null;
String inserSql = null;
Connection sqlLink = null;
final int PORT = 1239;
Socket client = null;
ClientHandler handler = null;
Scanner getTime;
try {
/**
* This piece of code has errors or you maybe did the error
* when copy/paste your code.
* Fixed the DriverManager connection code
*/
sqlLink = DriverManager.getConnection(
"correct info already here);
} catch (SQLException e) {
System.out.println("* Cannot connect to database! *");
System.exit(1);
}
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException ioEx) {
System.out.println("Unable to set up port!");
System.exit(1);
}
System.out.println("\nUp and running\n");
String currentTime = null;
getTime = new Scanner(System.in);
System.out.println("\nSet time for Ball");
currentTime = getTime.nextLine();
Items BallItem = new Items("Ball", currentTime, "Ball");
System.out.println("\nSet time for Plate");
currentTime = getTime.nextLine();
Items PlateItem = new Items("Plate", currentTime, "Plate");
Bidder bidder = new Bidder(null);
BallItem.start();
PlateItem.start();
Users user = new Users(BallItem, PlateItem);
ArrayList<ClientHandler> userList =
new ArrayList<ClientHandler>();
//add new array for the clients details as stated
ArrayList<String> bidders = new ArrayList<String>();
do {
client = serverSocket.accept();//
System.out.println("Enter username ");//
//takes in bidders name (1.1 and 2.1)
bidders.add(getTime.nextLine());
bidders.add(client.getInetAddress().toString());
//takes in next bidder's ip address to string like in tips (2.2)
Bidder userBidding = new Bidder(bidders);
System.out.println("\nUser" + (user.getUsers() + 1));
user.addUsers(userList, client, handler);
System.out.println(userBidding.returnUsers());
bidder.insert(statement, statement);
}
while (true);
}
}
class Items extends Thread {
private String name, description, timeUp, userBidding = "";
private boolean newBid = true;
private double topBid = 0;
private Calendar start = Calendar.getInstance();
private int date = start.get(Calendar.DATE); //gets the dates
private int month = start.get(Calendar.MONTH);
private int year = start.get(Calendar.YEAR);
private Calendar deadline = Calendar.getInstance();
//time file supplied to help etc
private Calendar now = Calendar.getInstance();
public Items(String newName, String newTimeString,
String newDescription) throws IOException {
String timeString, hourString, minString;
name = newName;
description = newDescription;
timeUp = newTimeString;
timeString = newTimeString;
hourString = timeString.substring(0, 2);
int hour = Integer.parseInt(hourString);
minString = timeString.substring(3, 5);
int minute = Integer.parseInt(minString);
deadline.set(year, month, date, hour, minute, 0); //setting time
}
public void run() {
while (now.before(deadline)) {
System.out.println(name + " " + getDateTime(now));
try {
Thread.sleep(5000); //increased to check server notifications easier
} catch (InterruptedException intEx) {
}
now = Calendar.getInstance();
}
System.out.println("\nDeadline! Times up!");
newBid = false;
}
class Bidder {
private static ArrayList<String> users;
private static Users user = new Users(null, null);
public Bidder(ArrayList<String> newUserList) {
users = newUserList;
}
public ArrayList<String> returnUsers()//return users as in question
{
return users;
}
public static String returnIP()//return ip as in question
{
return users.get(user.getUsers() + 1);
}
static void insert(Statement insert, Statement statement) {
Users user = new Users(null, null);
try {
String insertSql = "INSERT INTO Usernames(id, Name, ipAddress) VALUES (1"
+ users.get(user.getUsers()) + "," + returnIP() + ")";
System.out.println(users.get(user.getUsers()));
statement.executeUpdate(insertSql);
} catch (SQLException sqlEx) {
System.out.println("* Cannot execute insert! *");
sqlEx.printStackTrace();
System.exit(1);
}
}
//remove user as stated in question
public synchronized void RemoveUser(Socket client, int clientUser) {
int index = 0;
for (final String newString : users) {
if (users.indexOf(user.getUsers()) == clientUser) {
try {
client.close();
Thread.currentThread().isInterrupted();
users.remove(index);
} catch (final IOException e) {
System.out.println("Try to leave again!");
System.exit(1);
}
}
index++;
}
}
}
class Users {
private ArrayList<ClientHandler> userList;
private Items Ball;
private Items Plate;
public Users(Items newItem1, Items newItem2) {
Ball = newItem1;
Plate = newItem2;
userList = null;
}
public synchronized int getUsers() {
int totalUsers = 0;
if (userList != null)
for (ClientHandler handler : userList)
totalUsers++;
return totalUsers;
}
}// <-- miss this close brace