Aim: Read in the userID, check if the userID has 10-digits and check if it not exists in database, when both right, the program will leave the loop.
This code works on my project, but I think its not the most elegant solution. Have someone any improvements?
InputReader.java
public class InputReader {
private String userID;
public void readInput() throws SQLException {
boolean UserIDExists = false;
int existsValue = 0;
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("UserID (10-digits):");
while(!UserIDExists) {
this.userID = input.readLine();
while (this.userID.length() != 10) {
System.out
.println("Input must be 10 digits. Type again: :");
this.userID = input.readLine();
}
CustomerInfo customer = new CustomerInfo();
existsValue = customer.checkCustomer(this.userID);
if (existsValue == 1) {
UserIDExists = false;
System.out.println("UserID exists already. Type again:");
} else {
UserIDExists = true;
}
}
CustomerInfo.java
public class CustomerInfo {
private Connection conn = null;
private ResultSet resultSet = null;
public CustomerInfo () {
try {
conn = null;
JDBCConnection jdbcConn = new JDBCConnection();
conn = jdbcConn.openConnection();
} catch (Exception e) {
throw e;
}
};
public int checkUserID (String userID) throws SQLException {
PreparedStatement pstmt = null;
String sql = "SELECT * FROM TABL0001 WHERE USERID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userID);
resultSet = pstmt.executeQuery();
if (!resultSet.next()){
return 0;
}
return 1;
}