Java Setters 和 Getters 将数据存入 Java 数据库

发布于 2024-12-18 03:31:08 字数 321 浏览 2 评论 0原文

我正在开发一个独立的Java项目(不是Java EE),我目前正在学习JDBC,我正在创建一个数据库,其中包含员工信息,例如个人信息、联系信息、员工信息和税务信息,所有这些是相互引用的类,并且它们有 setter 和 getter,我想知道如何将它们的数据值插入到我在数据库中创建的数据库/表中?简而言之,我有 Employee 一个员工对象,类似于这样

Employee(PersonalInformation information,ContactInformation cInformation)

的东西,我如何将他们的数据添加到我在数据库中创建的表中?

I am working on a stand-alone Java project (not Java EE), I am currently learning JDBC, I am creating a database where it contains Employee information, such as Personal information, Contact Information, Employee Information and Tax information, all of these are classes with references with each other and they have setters and getters, I am wondering how would I insert their data value in the database/tables I created in the database? in short I have Employee an employee object with like this

Employee(PersonalInformation information,ContactInformation cInformation)

Something like that, how would I add their data in the tables I made in the database?

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

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

发布评论

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

评论(4

贱人配狗天长地久 2024-12-25 03:31:08

我如何将他们的数据添加到我在数据库中创建的表中?

通过使用 JDBC 将 INSERT 语句发送到数据库,对象图中的每个对象都有一个 INSERT 语句(假设每个类有一个表)。

是的,需要编写大量无聊、重复的代码,其中可能包含错误并且维护起来相当困难。

这就是为什么人们编写像 Hibernate 这样的对象关系映射器,并且有一个 Java 标准那个叫JPA。它是 Java EE 的一部分,但这并不意味着您必须运行 Java EE 服务器才能使用它。

更新:好的,所以你不能使用 ORM,可能是因为你应该首先了解 JDBC 是如何工作的,这是有道理的,因为 ORM 是基于 JDBC 的,有时你必须深入到该级别当出现问题时。

直接使用 JDBC 时,通常会创建一个 DAO(数据访问对象) 层,负责写入以及从数据库读取对象。为了处理嵌套对象,一个 DAO 可以调用另一个。

不,DAO 不会在 setter 和 getter 中调用。由应用程序对用户输入做出反应的部分调用。例如,当用户打开应用程序并且开始屏幕显示员工列表时,您将调用 EmployeeDAO.findAll() 方法,并且当用户对员工进行更改并单击“保存”时,您将调用 EmployeeDAO.findAll() 方法。 ”,您将调用 EmployeeDAO.save() 方法。

how would I add their data in the tables I made in the database?

By using JDBC to send INSERT statements to the database, one for each object in your object graph (assuming you have one table per class).

And yes, that's a lot of boring, repetitive code to write, which is likely to contain errors and quite a burden to maintain.

Which is why people have written Object-Relational mappers like Hibernate, and there's a Java standard for that called JPA. Which is part of Java EE, but that doesn't mean you have to run a Java EE server to use it.

Update: OK, so you cannot use an ORM, probably because you're supposed to understand how JDBC works first, which makes sense because ORMs are based on JDBC and sometimes you have to go down to that level when there is a problem.

When using JDBC directly, you typically create a DAO (data ccess object) layer that is responsible for writing and reading objects to/from the database. For dealing with a nested object, one DAO can call the other.

And no, the DAOs are not called in the setters and getters. There called by the part of your application that reacts to user input. E.g. when the user opens the application and the start screen shows a list of employees, you'd call an EmployeeDAO.findAll() method, and when the user makes changes to an employee and clicks on "save", you'd call the EmployeeDAO.save() method.

悲喜皆因你 2024-12-25 03:31:08

我建议您查看 Spring 的 JdbcTemplate (http://www.java2s.com/Code/Java/Spring/ControlParameterTypeInJdbcTemplateQuery.htm)

它看起来像这样:

SingleConnectionDataSource ds = new SingleConnectionDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:data/tutorial");
ds.setUsername("sa");
ds.setPassword("");

JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into employee (id, name) values (?, ?)", 1, "Tom");

每个 DAO 类实例使用一个 jdbcTemplate。

I recommend you to look at Spring's JdbcTemplate (http://www.java2s.com/Code/Java/Spring/ControlParameterTypeInJdbcTemplateQuery.htm)

Its looks like this:

SingleConnectionDataSource ds = new SingleConnectionDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:data/tutorial");
ds.setUsername("sa");
ds.setPassword("");

JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into employee (id, name) values (?, ?)", 1, "Tom");

Use one jdbcTemplate per DAO class instance.

来日方长 2024-12-25 03:31:08

这可以帮助您

  • 将所有必需的值传递给相应的对象。
  • 获取它们的所有值并使用这些值构建查询,例如

     Employee em = new Employee(perinfo, continfo);
     字符串 emp_per_info = em.getSomeInfo(); // 它返回一个字符串
     ...
     //很快
     query =“插入表名值(变量放此处);”
    

    让我知道它是否不起作用

This may help you

  • Pass all the required values to the respective objects.
  • get all their values and frame a query using those values like

     Employee em = new Employee(perinfo, continfo);
     String emp_per_info = em.getSomeInfo();   // it this returns a string
     ...
     //so on
     query="insert into table_name values(variables go here);"
    

    let me know if it didn't work

琉璃梦幻 2024-12-25 03:31:08

您正在尝试做的事情称为“对象到关系映射”。您拥有构成对象域的 java 对象,以及构成关系模型的 DB 表。你已经把它们联系起来了。保存数据并查询以取回它们。
您有两种方法来编写自己的有限框架或使用现有框架。现有框架包括:

  • Hibernate
  • iBatis
    等等。

要自己做一项,最简单的方法是将简单的结果集写入对象映射器并创建插入字符串。
我建议您研究一些 ORM 文档以了解更多信息。
问候

What you are trying to do is called "Object to Relational Mapping". You have the java Objects which make up your object domain and there is the DB Table which make up the relational model. You have relate them. Save the data and query to get them back.
You have two approaches to write a kind of your own limited framework or use an existing framework. Existing frameworks include :

  • Hibernate
  • iBatis
    and so on.

To do one on your own, simplest way to do would be writing simple result set to object mappers and creating insert strings.
I would suggest you study some ORM docs to know more.
Regards

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