mysql 有没有可能在int中插入字母?

xvw2m8pv  于 2022-10-31  发布在  Mysql
关注(0)|答案(5)|浏览(241)

我很困惑,我被要求使用OOP和DAO从Java连接到MySQL,但我的教授要求我们按以下方式进行:
我们需要将变量“MethodOfPayment”在Java中设置为int,在MySQL表中设置为char,并且我们需要根据您在Java中输入的数字来编写支付方法。例如:
Java:付款方式:(你写)1将在MySQL中插入“信用卡”
Java:付款方式:(你写)2将在MySQL中插入“借记卡”
但使用的是int MethodOfPayment变量。
我尝试了一个开关,但它不允许我将int“With”字母转换为string,我甚至不知道这是否可能。
这是我在DAO方法类中的插入

private static final String SQL_INSERT = "INSERT INTO Client(Name, LastName, MethodOfPayment ) VALUES (?,?,?)";

我使用ResultSet、ArrayList、PreparedStatement以及到MySQL的JDBC连接来完成此操作。

public static int = MethodOfPayment;

这个变量将写入数据库,我的教授要求我们在java中保存为int,并在MySQL中写入char。
这是我试图将int转换为string的方法,但显然崩溃了,因为int中的字母。我不知道这是可能的,还是我的教授错了。

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
    }
}

DAO方法:
Person是一个父类,包含name和Last。我从一个子类中获取TypeOfPayment,这个子类包含get、set和stringbuilder,并使用super来获取person数据

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;       
}
vwkv1x7d

vwkv1x7d1#

我已经修改了你的程序。Java变量的命名约定是lowerCamelCase,学习愉快!

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; // 
            }
        }
    }
}

在回复@sahm注解结帐this时回答String和int之间的转换。

mf98qq94

mf98qq942#

有多种方法可以实现这一点。
1.如果您使用的是Hibernate,则可以使用@Type注解来实现
1.如果您使用的是JPA,那么您可以使用convert来实现,它比其他方法更简单。
1.如果使用简单JDBC而不使用实体Map,则在执行查询前保存时,根据用户输入的数字将参数设置为debit、credit。例如:

stmt.setString(3, person.getInt_TypeOfPayment() == 2 ? "debit" : "credit" );

在检索数据时,遍历结果集,根据数据库中的字符串将value设置为int,示例代码如下。

ResultSet rs = stmt.executeQuery(QUERY);
while(rs.next()){
    ....
    int payment = rs.getString("your column name / index") == "debit" ? 2 : 1;
    person.setIntTypeOfPayment(payment);
}
1szpjjfi

1szpjjfi3#

如果您需要在Java中将char转换为int,请使用以下方法之一:

  • 隐式类型转换//获取ASCII值
  • 字符.getNumericValue()
  • Integer.parseInt()//与成对的
  • 减去'0' //仅适用于整数数值

您也可以执行显式类型转换。但是,这是一个冗余操作:它在Java中是可以工作的。

egdjgwm8

egdjgwm84#

您可以使用Guava库中的一个Ints方法,该方法与Java8的Optional相结合,可以提供一种将字符串转换为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();
     }
}
ivqmmu1c

ivqmmu1c5#

考虑使用连接(“字符串“+变量+“字符串“),或使用StringBuilder。https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

相关问题