更新:我尝试硬编码密码并让用户输入,它只是跳过了输入部分,如下所示:
Please press one to sign in or two to sign up if you haven't already1
Please enter your credentials below
Username:
Password:
Your username and/or password are incorrect, access has been denied
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MultipleClient {
public static void main(String[] args) throws IOException {
try {
Scanner scan = new Scanner(System.in);
InetAddress ip = InetAddress.getByName("localhost");
Socket socket = new Socket(ip, 8189);
// Receiving input and sending output to Server
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.println(inputFromServer.readUTF());
int choice = scan.nextInt();
outputToServer.writeInt(choice);
System.out.println(inputFromServer.readUTF());
String username = scan.nextLine();
outputToServer.writeUTF(username);
System.out.println(inputFromServer.readUTF());
String password = scan.nextLine();
outputToServer.writeUTF(password);
System.out.println(inputFromServer.readUTF());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class MultipleServer {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
ServerSocket serverSocket = new ServerSocket(8189);
// Server keeps on receiving new Clients
while (true) {
Socket clientSocket = null;
try {
// ServerSocket waits for a Client to connect
clientSocket = serverSocket.accept();
System.out.println("A new client is connected : " + clientSocket);
// Receiving input and sending output to Client
DataInputStream inputFromClient = new DataInputStream(clientSocket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(clientSocket.getOutputStream());
System.out.println("Assigning new thread for this client");
System.out
.println("-----------------------------------------------------------------------------------");
// Create a new Thread object for the Client
Thread thread = new ClientHandler(clientSocket, inputFromClient, outputToClient);
thread.start();
} catch (Exception e) {
clientSocket.close();
e.printStackTrace();
}
}
}
}
// ClientHandler class
class ClientHandler extends Thread {
final Socket clientSocket;
final DataInputStream inputFromClient;
final DataOutputStream outputToClient;
// Constructor
public ClientHandler(Socket clientSocket, DataInputStream inputFromClient, DataOutputStream outputToClient) {
this.clientSocket = clientSocket;
this.inputFromClient = inputFromClient;
this.outputToClient = outputToClient;
}
@Override
public void run() {
// Variables
String Username = "";
String Password = "";
int choice;
String toreturn;
while (true) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test" + "?user=root&password=test");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
try {
outputToClient.writeUTF("Please press one to sign in or two to sign up if you haven't already");
choice = inputFromClient.readInt();
if (choice != 1 && choice != 0) {
outputToClient.writeUTF("Please choose one of the listed choices");
} else {
if (choice == 1) {
//here is where the problem is occuring
outputToClient.writeUTF("Please enter your credentials below\n");
outputToClient.writeUTF("Username:");
Username = inputFromClient.readUTF();
outputToClient.writeUTF("Password:");
Password = inputFromClient.readUTF();
String queryoneresult = "";
//execution isn't reaching this part so I don't think it is relevant but I included it anyway
ResultSet rs = null;
PreparedStatement stmt = conn
.prepareStatement("SELECT COUNT(Username)\n" + " FROM Authentication\n"
+ " WHERE Username = ? AND\n" + " Password = ? \n" + " LIMIT 0, 1");
stmt.setString(1, Username);
stmt.setString(2, Password);
rs = stmt.executeQuery();
if (stmt.execute()) {
rs = stmt.getResultSet();
}
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1)
System.out.print(" ");
String columnValue = rs.getString(i);
queryoneresult = rs.getString(i);
}
}
if (Integer.valueOf(queryoneresult) == 1) {
outputToClient.writeUTF("Username and password have been verified, you can now proceed ");
} else {
outputToClient
.writeUTF("Your username and/or password are incorrect, access has been denied");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Please press one to sign in or two to sign up if you haven't already
1
Please enter your credentials below
Username:
sergio
Password:
Your username and/or password are incorrect, access has been denied
因此,服务器似乎自动获得对密码的响应,并像这样进行,执行也像这样:
Please press one to sign in or two to sign up if you haven't already
1
Please enter your credentials below
Username:
Password:
sergio
Your username and/or password are incorrect, access has been denied
如有任何帮助,不胜感激。
只需将这个scan.next()
放在它跳过输入的行上方,字符串password=scan.nextline();在本例中
。
System.out.println(inputFromServer.readUTF());
scan.next(); // Fake input
String password = scan.nextLine();
在服务器端也是:
inputFromClient.readUTF(); // fake input
Password = inputFromClient.readUTF();
问题内容: 我想编写一个程序,获取多个行输入并逐行处理它。为什么没有像Python 3那样的函数? 不允许用户使用换行符()分隔行,它仅打印回第一行。 可以将其存储在变量中,甚至可以将其读取到列表中吗? 问题答案: 在Python 3.x中,Python 2.x的功能已被替换。但是,在两种情况下,您都无法输入多行字符串,为此,您需要从用户行中逐行获取输入,然后使用来进行输入,或者您也可以采用多种行
我有这个项目和一些关于如何做的问题。下一步()。nextInt()。hasNext()和。hasNextInt()是Scanner类工作的一部分。提前感谢您的任何帮助:) 1/什么时候!安慰hasNextInt()是第一次执行,为什么它会要求输入? 我一开始以为控制台是空的,所以!console.hasNextInt()是True(空不是int),然后它应该直接从“请输入您的年龄:”转到“请重新输
问题内容: 我正在使用Java的扫描仪读取用户输入。如果我仅使用nextLine一次,那就可以了。使用两个nextLine时,第一个不会等待用户输入字符串(第二个会输入)。 输出: X:Y :(等待输入) 我的密码 任何想法为什么会发生这种情况?谢谢 问题答案: 您可能像以前那样调用方法。这样的程序是这样的: 演示您所看到的行为。 问题是不消耗,因此下一个调用将消耗它,然后等待读取的输入。 您需要
如果我在Instagram上有粉丝关注我的内容,并且在网上也有注册用户,有没有办法将两者联系起来? 有没有办法发现Instagram上的一个用户是否喜欢一张照片,并看看那个用户是否在我的网站上注册了?
我需要从用户给出的输入日期打印日历。然而,我不允许使用任何预定的日期类。 目前,我可以打印月份和年份,但是,我似乎不知道如何打印特定月份和年份的日期。我对Java非常陌生,因此非常感谢您的帮助! 以下是所需输出的示例: 以下是我到目前为止的输出: 以下是我迄今为止所尝试的:
基本上,我正在使用TestNG开发Selenium automation,我想为一个字段获取用户输入,而要获取用户输入,我需要scanner类的帮助。但如果没有主要阶层,它就不起作用。有人能帮我吗?