简体   繁体   English

使用Java获取MySQL表中的所有主键

[英]Get all primary keys in a MySQL table using Java

How could I get the names of all primary keys in a MySQL table using Java ? 如何使用Java MySQL表中所有主键的名称? Let's say I have a table: 假设我有一张桌子:

Name │ Phone Number │ verified
─────┼──────────────┼─────────
John │ 871-123-1842 │    0
Mark │ 912-917-8172 │    1
Matt │ 182-134-9917 │    1
Jake │ 971-991-8264 │    1

I would like to get the values John , Mark , Matt , and Jake from the table. 我想从表中获取值JohnMarkMattJake

I know there could be an alternative of just having a list of all of all of the primary keys, and adding to it whenever a new one is added, just I'm looking for something simpler, that will take up less space. 我知道可以有一种选择,就是只列出所有所有主键,并在添加新主键时将其添加到其中,只是我在寻找更简单的东西,它将占用更少的空间。 I'm looking for something like: 我正在寻找类似的东西:

PreparedStatement query = mySqlConnection.prepareStatement("SELECT * FROM `table`");
ResultSet results = query.executeQuery();
List<Object> results = results.getPrimaryKeyValues(); //I don't know what to put here

So, how could I achieve getting an Array, or something that I can turn into an Array, of all of the primary keys inside of a MySQL table using Java ? 那么,如何使用Java MySQL表中所有主键的数组或可以变成数组的东西呢?

First off, I suppose you don't know what columns are Primary Keys on your table. 首先,我想您不知道表上哪些列是主键。 The first step would be to get these column names, and then get the values. 第一步将是获取这些列名称,然后获取值。

To get the column names: 要获取列名:

public ArrayList<String> findPrimaryKeyColumnNames(Connection connection, 
                 String catalog, String schema, String table){ 
    /* open the connection*/
    ...

    ArrayList<String> columns = new ArrayList<String>();
    DatabaseMetaData dbData = connection.getMetaData();
    Resultset result = dbData.getPrimaryKeys(catalog, schema, table);
    while (result.next())
        columns.add(result.getString("COLUMN_NAME"));

    ... 
    /* you should... PROBABLY... close your connection ;) */

    return columns;
}

What this method does is it returns an ArrayList containing the names of the columns that are Foreign Keys from the table you put in your parameter. 此方法的作用是返回一个ArrayList,其中包含作为参数表中外键的列的名称。

With this new info, you can easily make a dynamic query that goes like: 使用此新信息,您可以轻松进行如下动态查询:

String query = "SELECT ";
for(String column : columns) //columns contains the info from the first method
    query += column + ", ";
query = query.substring(0, query.length - 2); //you want to get rid of that extra comma
query += "FROM " + tableName;

You can then recover the data from this query and it will only contain your primary keys for a given table. 然后,您可以从该查询中恢复数据,并且该数据仅包含给定表的主键。

There is no method like getPrimaryKeyValues() . 没有像getPrimaryKeyValues()这样的方法。

You need to iterate through the ResultSet to create your own list of values. 您需要遍历ResultSet来创建自己的值列表。 Something like: 就像是:

String sql = "Select Name from yourTableNameHere";

Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql );
List<String> names = new ArrayList<String>();

while (rs.next())
{
    names.add( rs.getString(1) );
}

rs.close();
stmt.close();

System.out.println( names );
 DatabaseMetaData databaseMetaData = conn.getMetaData();  

 ResultSet resultSet = databaseMetaData.getPrimaryKeys(null, null ,  tablename);          

while (resultSet.next()) {
                String primarykeys= resultSet.getString(4);
                System.out.println(primarykeys);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM