如何将从数据库收集的数据存储在对象数组中?

发布于 2024-09-15 05:51:10 字数 2123 浏览 3 评论 0原文

在这里,我编写了从数据库获取数据的代码。我想将数据存储在对象数组(POJO)中。 怎么做?这段代码也可以将数据插入DB,但是省略了。

import java.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jdbc {
static int id[] = new int[10];
static String name[] = new String[10];
static int salary[] = new int[10];
public static void main(String arg[])
{
    try {
          Statement stmt;
          ResultSet rs;
          Class.forName("com.mysql.jdbc.Driver");
          String url ="jdbc:mysql://localhost:3306/dwr";
          Connection con = DriverManager.getConnection(url,"root", "1234");
          System.out.println("URL: " + url);
          System.out.println("Connection: " + con);
          stmt = con.createStatement();
          System.out.println("Enter EmpId:");
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          int empId=Integer.parseInt(br.readLine());
          System.out.println("Enter Name:");
          BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
          String name1=br1.readLine();
          System.out.println("Enter Salary:");
          BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
          int salary1=Integer.parseInt(br2.readLine());
          stmt.executeUpdate("INSERT INTO employee set EmpId='"+empId+"', Name='"+name1+"', salary='"+salary1+"';");
          stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
          rs = stmt.executeQuery("SELECT * " + "from employee ORDER BY EmpId");
          System.out.println("Display all results:");
          int i = 0;
          while(rs.next()){
            id[i]= rs.getInt("Empid");
            name[i]= rs.getString("Name");
            salary[i]= rs.getInt("Salary");
            System.out.println("\n EmpId " + id[i]+ "\n name " + name[i] + "\n salary " + salary[i]);
          }

    }catch( Exception e ) {
          e.printStackTrace();
}
    Jdbc pojo = new Jdbc();

}
}

Here, i have coded to get data from DB. I want to store the data in Object Array(POJO).
How to do it?
This code can also insert Data into DB, but omit it.

import java.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jdbc {
static int id[] = new int[10];
static String name[] = new String[10];
static int salary[] = new int[10];
public static void main(String arg[])
{
    try {
          Statement stmt;
          ResultSet rs;
          Class.forName("com.mysql.jdbc.Driver");
          String url ="jdbc:mysql://localhost:3306/dwr";
          Connection con = DriverManager.getConnection(url,"root", "1234");
          System.out.println("URL: " + url);
          System.out.println("Connection: " + con);
          stmt = con.createStatement();
          System.out.println("Enter EmpId:");
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          int empId=Integer.parseInt(br.readLine());
          System.out.println("Enter Name:");
          BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
          String name1=br1.readLine();
          System.out.println("Enter Salary:");
          BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
          int salary1=Integer.parseInt(br2.readLine());
          stmt.executeUpdate("INSERT INTO employee set EmpId='"+empId+"', Name='"+name1+"', salary='"+salary1+"';");
          stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
          rs = stmt.executeQuery("SELECT * " + "from employee ORDER BY EmpId");
          System.out.println("Display all results:");
          int i = 0;
          while(rs.next()){
            id[i]= rs.getInt("Empid");
            name[i]= rs.getString("Name");
            salary[i]= rs.getInt("Salary");
            System.out.println("\n EmpId " + id[i]+ "\n name " + name[i] + "\n salary " + salary[i]);
          }

    }catch( Exception e ) {
          e.printStackTrace();
}
    Jdbc pojo = new Jdbc();

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-09-22 05:51:10

您可以使用提供的 get 方法一次填充对象的字段,因此,如果您有一个名为 employee 的对象,它接受 3 个参数(id、姓名和薪水),您可以或者

List<Employee> employee = new ArrayList<Employee>();
while(rs.next())
{
employee.add(new Employee(rs.getInt("Empid"), rs.getString("Name"),rs.getInt("Salary"));
}

,您可以使用 ORM 工具,例如 Hibernate 并使用它来检索此处所示的对象

PS 我提供的 Java 代码更像是伪代码。我还没有测试过。

You can either use the get methods provided to populate the fields of the object one at a time, so if you have an object named employee, that takes in 3 arguments (id, name and salary), you can have something like this:

List<Employee> employee = new ArrayList<Employee>();
while(rs.next())
{
employee.add(new Employee(rs.getInt("Empid"), rs.getString("Name"),rs.getInt("Salary"));
}

Or else, you can use an ORM Tools, like Hibernate and use it to retrieve objects as shown here

P.S. The Java code I have provided is more like Pseudo code. I have not tested it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文