I’m confused, I was asked to do a connection from Java into MySQL using OOP and DAO, but my professor asked us to do it in a the following way:
We need to make the variable "MethodOfPayment" as an int in Java and as a char in MySQL table, and we need to write the method of payment depending on the number you put in Java. For example:
Java: MethodOfPayment: (you write) 1 will insert "Credit card" in MySQL
Java: MethodOfPayment: (you write) 2 will insert "Debit card" in MySQL
but using the int MethodOfPayment variable.
I tried a switch, but it won’t let me convert the int "With" letters to string, I don’t even know if it’s possible.
This is the insert I have in the DAO method class
private static final String SQL_INSERT = "INSERT INTO Client(Name, LastName, MethodOfPayment ) VALUES (?,?,?)";
I do it with ResultSet, ArrayList, PreparedStatement with a JDBC connection to MySQL.
public static int = MethodOfPayment;
This is the variable that will write on the database an the one my professor is asking us to keep as an int in java and write the char on MySQL.
This is the method I’m trying to convert the int to string, but obviously crashes because the letters inside the int. I don’t know if it’s possible or my professor is wrong.
public static void PaymentMethod() {
int MethodSelection; //this is a variable to select the method in the switch and assign the values to the main variable of the payment
System.out.println("Insert Payment Method");
Scanner sc = new Scanner(System.in);
MethodSelection = Integer.parseInt(sc.nextLine());
switch (MethodSelection) {
case 1:
MethodOfPayment = Integer.parseInt("Debit");
break;
case 2:
MethodOfPayment = Integer.parseInt("Credit Card");
break;// ...
default:
System.out.println("Invalid Method, Try Again");
PaymentMethod(); // I don't know a way to restart the switch without restarting the whole method when another value is inserted
}
}
DAOmethod:
Person is the father clas with, name and Last name. I get TypeOfPayment from a son class that has get, set and stringbuilder with the super to get the person data
public int insert(Client person){
Connection conn = null;
PreparedStatement stmt = null;
int registros = 0;
try {
conn = getConnection();
stmt = conn.prepareStatement(SQL_INSERT);
stmt.setString(1, person.getStr_Name());
stmt.setString(2, person.getStr_Lastname);
stmt.setInt(3, person.getInt_TypeOfPayment());
archives= stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace(System.out);
}finally{
try {
close(stmt);
close(conn);
} catch (SQLException ex) {
}
}
return archives;
}
5
Answers
Consider using concatenation (" string " + variable + " string "), or using a StringBuilder. https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
I have modified your program. Java naming conventions for variable is lowerCamelCase, happy learning!
If you need to convert char to int in Java use one of the methods:
You can also do explicit type casting. However, this is a redundant operation: it is not needed, but just works in Java.
you can use an Ints method from the Guava library, which in combination with Java 8’s Optional, makes for a powerful and concise way to convert a string into an int:
There are multiple ways to achieve this.
While retrieving data, iterate over result set and set value as int based on the string which comes from data base. Sample code as follows.