如何使用 Java 应用程序在 SQL 数据库中执行搜索?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21518917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:07:10  来源:igfitidea点击:

How to perform a search in an SQL database with a java application?

javamysqldatabasesearch

提问by Storio

I'm planning to build a very very simple java application done on Netbeans that accepts basic individual information like ID number, name, and address and stores it on an sql database.

我计划在 Netbeans 上构建一个非常简单的 java 应用程序,它接受基本的个人信息,如 ID 号、姓名和地址,并将其存储在 sql 数据库中。

I want to put a search function on my program that accepts ID numbers. When the user inputs the ID number that is stored in the database, it will show the Name and address on a pop up message dialog.

我想在我的程序上放置一个接受 ID 号的搜索功能。当用户输入存储在数据库中的 ID 号时,它将在弹出的消息对话框中显示名称和地址。

I know this is possible but can you link me to some guides or documentations about the search function? or maybe you could give me a very short example of a sample code done in the search button?

我知道这是可能的,但是您能否将我链接到有关搜索功能的一些指南或文档?或者您可以给我一个在搜索按钮中完成的示例代码的非常简短的示例?

采纳答案by Scary Wombat

Have a look at this link

看看这个链接

It is using classes such as Connectionand PreparedStatement

它正在使用诸如ConnectionPreparedStatement

Pseudo-code being

伪代码存在

String selectSQL = "SELECT USER_ID, USERNAME FROM DBUSER WHERE USER_ID = ?";

dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);

// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();

try {
  while (rs.next()) {
    String userid = rs.getString("USER_ID");
    String username = rs.getString("USERNAME");
    System.out.println("userid : " + userid);
  }

  } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
  } finally {
    preparedStatement.close();
    dbConnection.close();
  }

回答by therealrootuser

http://docs.oracle.com/javase/tutorial/jdbc/basics/

http://docs.oracle.com/javase/tutorial/jdbc/basics/

Look into how to use JDBC.

研究如何使用 JDBC。

A very basic example:

一个非常基本的例子:

    Connection c = null; //need to initialize a java.sql.Connection from JDBC.


    String sql = "SELECT * FROM Users WHERE name = ?";        
    PreparedStatement ps = c.prepareStatement(sql);
    ps.setString(1, "John Smith");

    ResultSet rs = ps.executeQuery();

    List<String> matchingNames = new ArrayList<>();
    while (rs.next())
    {
        String name = rs.getString("name");
        matchingNames.add(name);
    }       

    for (String name: matchingNames)
    {
        //Display dialog.
    }

回答by Doug Hou

This linkis a very good JDBC tutorial. JDBC is the way that java uses database, but this is so basic that almost no real project use JDBC directly.

这个链接是一个非常好的 JDBC 教程。JDBC 是 java 使用数据库的方式,但这是非常基本的,几乎没有真正的项目直接使用 JDBC。

If you want to learn more, try MyBatisand Hibernatewhich are the most popular ORM framework, and both of them are based on JDBC.

如果你想了解更多,可以试试最流行的 ORM 框架MyBatisHibernate,它们都是基于 JDBC 的。

PS. http://www.mkyong.com/this site has many good tutorials for Java developer

附注。http://www.mkyong.com/这个站点有很多 Java 开发者的好教程