skip to Main Content

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


  1. Consider using concatenation (" string " + variable + " string "), or using a StringBuilder. https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

    Login or Signup to reply.
  2. I have modified your program. Java naming conventions for variable is lowerCamelCase, happy learning!

    package test1;
    
    import java.util.Scanner;
    
    public class Snippet {
        public static void PaymentMethod() {
            int methodSelectionInt;// this is a variable to select the method in the swich and asign the values to
                                    // the main variable of the payment
            String methodOfPayment;// the database variable of the payment
            Boolean tryAgain = false;
            while (tryAgain) { 
                
                System.out.println("Insert Payment Method");
                Scanner sc = new Scanner(System.in);
                methodSelectionInt = Integer.parseInt(sc.nextLine());
                switch (methodSelectionInt) {
                case 1:
                    methodOfPayment = "Debit";
                    tryAgain = false; 
                    break;
                case 2:
                    methodOfPayment = "Credit Card";
                    tryAgain = false; 
                    break;// ...
                default:
                    System.out.println("Invalid Method, Try Again");
                    tryAgain = true; // 
                }
            }
        }
    }
    
    Login or Signup to reply.
  3. If you need to convert char to int in Java use one of the methods:

    • Implicit type casting //getting ASCII values
    • Character.getNumericValue()
    • Integer.parseInt() //in pair with
    • Subtracting ‘0’ //works for integer numeric values only

    You can also do explicit type casting. However, this is a redundant operation: it is not needed, but just works in Java.

    Login or Signup to reply.
  4. 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:

     import com.google.common.primitives.Ints;
    
     public static void PaymentMethod() {
    
         int MethodSelection;//this is a variable to select the method in the swich and asign 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 = Optional.ofNullable("Debit").map(Ints::tryParse).orElse(0);
                 break;
    
             case 2:
                 MethodOfPayment = Optional.ofNullable("Credit Card").map(Ints::tryParse).orElse(0);
                 break;
    
             default:
                 System.out.println("Invalid Method, Try Again");
                 PaymentMethod();
         }
    }
    
    Login or Signup to reply.
  5. There are multiple ways to achieve this.

    1. if you are using Hibernate you can do it with @Type annotation
    2. If you are using JPA then you can do it with convert. It is simpler among all.
    3. If you are using simple JDBC and not using entity mapping, then while saving before executing query set parameters as debit, credit based on number entered by user. For example:
      stmt.setString(3, person.getInt_TypeOfPayment() == 2 ? "debit" : "credit" );
      

    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.

    ResultSet rs = stmt.executeQuery(QUERY);
    while(rs.next()){
        ....
        int payment = rs.getString("your column name / index") == "debit" ? 2 : 1;
        person.setIntTypeOfPayment(payment);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search