如何将PreparedStatement设置的输入值存入全局变量并跨类使用
把用户输入存到公共变量供其他Java类调用的简单方法
你现在的代码是直接把用户输入传给PreparedStatement,没把输入内容保存下来。要实现其他类能调用这些输入,分两步来:先把输入存到局部变量,再把局部变量放到其他类能访问的地方。
第一步:先把用户输入存到局部变量
先别直接把input.nextLine()塞给PreparedStatement,先存到自己的变量里,后面再用:
String sql = "insert into customer(customerName, customerAddress, column_name) values(?,?,?)"; System.out.println(); System.out.println("___________________________Hello Customer ___________________________"); PreparedStatement preparedStatement = connection.prepareStatement(sql); // 先把姓名存到局部变量,再传给PreparedStatement System.out.print("Enter Your Name : "); String customerName = input.nextLine(); preparedStatement.setString(1, customerName); // 地址同理 System.out.print("Enter Your Address : "); String customerAddress = input.nextLine(); preparedStatement.setString(2, customerAddress);
第二步:把变量变成其他类能访问的公共变量
这里给你两种简单好懂的方法,适合新手:
方法1:用静态变量存(最简单)
新建一个专门存公共数据的类,比如叫CustomerData,里面放公共静态变量——静态变量不用创建对象,直接用类名就能调用。
// 新建的CustomerData类 public class CustomerData { // 公共静态变量,其他类直接能读能改 public static String customerName; public static String customerAddress; }
然后回到原来的代码,把局部变量赋值给这些静态变量:
// 存姓名的时候顺便赋值给公共变量 System.out.print("Enter Your Name : "); String customerName = input.nextLine(); preparedStatement.setString(1, customerName); CustomerData.customerName = customerName; // 地址同理 System.out.print("Enter Your Address : "); String customerAddress = input.nextLine(); preparedStatement.setString(2, customerAddress); CustomerData.customerAddress = customerAddress;
其他类要调用的时候,直接写:
// 比如在另一个类里获取用户姓名 String name = CustomerData.customerName; System.out.println("用户姓名:" + name);
方法2:用实体类存(更规范,推荐)
如果以后要存更多用户信息(比如电话、邮箱),用实体类封装更清晰。新建一个Customer类,把用户信息打包成一个对象:
// 新建的Customer实体类 public class Customer { // 私有变量,不能直接改,用get/set方法操作(更安全) private String customerName; private String customerAddress; // 获取姓名的方法 public String getCustomerName() { return customerName; } // 设置姓名的方法 public void setCustomerName(String customerName) { this.customerName = customerName; } // 获取地址的方法 public String getCustomerAddress() { return customerAddress; } // 设置地址的方法 public void setCustomerAddress(String customerAddress) { this.customerAddress = customerAddress; } }
然后在原来的代码里,创建Customer对象,把输入存进去:
// 先创建一个Customer对象 Customer customer = new Customer(); System.out.print("Enter Your Name : "); String customerName = input.nextLine(); preparedStatement.setString(1, customerName); customer.setCustomerName(customerName); // 把姓名存到对象里 System.out.print("Enter Your Address : "); String customerAddress = input.nextLine(); preparedStatement.setString(2, customerAddress); customer.setCustomerAddress(customerAddress); // 把地址存到对象里
要让其他类用这个对象,有两种方式:
- 把
customer对象作为参数传给其他类的方法(比如OtherClass.processCustomer(customer)); - 也可以像方法1那样,在
CustomerData里加个public static Customer customer;,把对象存进去,其他类直接调用CustomerData.customer.getCustomerName()。
其他类调用示例:
// 假设拿到了customer对象 String name = customer.getCustomerName(); String address = customer.getCustomerAddress(); System.out.println("用户:" + name + ",地址:" + address);
注意事项
- 静态变量是全局共用的,如果同时有多个用户输入,后面的会覆盖前面的,适合单用户场景;
- 实体类的get/set方法是Java的规范写法,能避免误改数据,更安全。
内容的提问来源于stack exchange,提问作者Himath Kariyawasam
相关产品推荐
相关产品推荐

