How could I get the names of all primary keys in a MySQL table using Java? 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.
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?
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.
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().
You need to iterate through the ResultSet to create your own list of values. 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);
}
Related
I am developing a software in Java, what I am doing is searching a value in full sqlite database (55 Columns and 1000 rows) and updating it.
I can find that value in the database but when I am try to update it I want to skip columns which have only NULL values. Using ResultSet it automatically skips other columns which have non-null values.
How can I check if ResultSet is not skipping the columns, finding the desired column and updating that value?
What I tried is:
ArrayList<String> arr = new ArrayList<String>() ;
rs = statement.executeQuery(); // that is the select statement
//Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and other columns like this too...
List<String> cols = Arrays.asList(getColLabels());
// I am getting Column Labels from ResultSetMetaData and it works fine
while(rs.next()){
for(String column: cols){
String value;
if(rs.getBoolean(column)){
//That is the problematic area, if ResultSet finds a column with full of NULL values it skips to next column.
// But other columns it skips too , which have not null Values and after that ResultSet does not find a proper column.
if((value = rs.getObject(column).toString()).matches(".*"+searchedVal+".*") ){
//searchedVal is searched value
arr.add("UPDATE Table_Name SET "+column+" ='"+newValue+"' WHERE "+ column+" ='" +value+"';" );
// then send array in another function to update to database
break;
}
}
}
}
Thank you for your help.
Please try to solve the problematic area with the data tier(SQL command), not the business trie(java code).
You want to have rows which all of the columns must be non null? so simply add more condition clauses(not null) to your sql command
something like:
Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and column_n like %VALUE% and (column1 not null and column2 not null and column_n not null)
I found the answer,
rs.getBoolean does not work with different type of column types, so I changed getObject function.
Working code is:
ArrayList<String> arr = new ArrayList<String>() ;
rs = statement.executeQuery(); // that is the select statement
//Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and other columns like this too...
List<String> cols = Arrays.asList(getColLabels());
// I am getting Column Labels from ResultSetMetaData and it works fine
while(rs.next()){
for(String column: cols){
String value;
if(rs.getObject(column) != null){
if((value = rs.getObject(column).toString()).matches(".*"+searchedVal+".*") ){
//searchedVal is searched value
arr.add("UPDATE Table_Name SET "+column+" ='"+newValue+"' WHERE "+ column+" ='" +value+"';" );
// then send array in another function to update to database
break;
}
}
}
}
I have a table called [Elenco_Aziende] from which I extract all the record in a resultset. [Elenco_Aziende] is in a relation one to many with other two tables called [Elenco_Autisti] and [Elenco_Veicoli] via a field called [Partita_IVA_Azienda] that is also primary key in [Elenco_Aziende] table.
After extracting all records from [Elenco_Aziende] I perform a loop for each value of [Partita_IVA_Azienda] and then open a new result set to try to read values in related fields of [Elenco_Autisti] and [Elenco_Veicoli] tables and do for each of them some operation.
And here comes strange thing: As long as [Partita_IVA_Azienda] (which is defined as a Text field in the Access DB) values are all the same length everything is OK when I try to read in [Elenco_Autisti] and [Elenco_Veicoli] tables, but if some of [Partita_IVA_Azienda] has a different length then I got error:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.1 data exception: numeric value out of range
More precisely here is the nested loop scenario:
Connection con = DriverManager.getConnection("jdbc:ucanaccess://"
+ filepath);
String qry = "SELECT * FROM Elenco_Aziende";
ResultSet rs = stmt.executeQuery(qry);
String cognometest = "";
String nometest ="";
while (rs.next()) {
String partitaiva = "Partita IVA: "
+ rs.getString("Partita_IVA_Azienda") + "\n\r";
String partitaivazienda = rs.getString("Partita_IVA_Azienda");
Statement stmtautisti = con.createStatement();
System.out.println("Sto per eseguire la query per partita iva azienda = " + partitaivazienda + "\n\r");
String qryautisti = "SELECT * FROM Elenco_Autisti WHERE Partita_IVA_Azienda="
+ partitaivazienda; /*!!!!! AND HERE WHEN I EXECUTE NEXT QUERY IS WHERE I GET THE EXCEPTION net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.1 data exception: numeric value out of range more!!!!!*/
ResultSet rsautisti = stmtautisti.executeQuery(qryautisti);
while (rsautisti.next()) {
do something here
}
Statement stmtveicoli = con.createStatement();
String qryveicoli = "SELECT * FROM Elenco_Veicoli WHERE Partita_IVA_Azienda="
+ rs.getString("Partita_IVA_Azienda");
ResultSet rsveicoli = stmtveicoli.executeQuery(qryveicoli);
while (rsveicoli.next()) {
do something else here
}
that is as soon as I execute the query
String qryautisti = "SELECT * FROM Elenco_Autisti WHERE Partita_IVA_Azienda="+ partitaivazienda;
for a different length value of [Partita_IVA_Azienda] I get the problem.
I even tried to export the database in a comma separated value and reimporting it in a brand new one but it did not help. Furthermore, the problem seems to happen just for large number of records in tables [Elenco_Autisti] (138 records) and [Elenco_Veicoli] (287 records), while seems not to happen for small number of records. [Elenco_Aziende] is small (no more than 10 records).
According to the little of what I know about SQL, a WHERE with a text field should be written with the value in apostrophes:
String qryautisti = "SELECT * FROM Elenco_Autisti WHERE Partita_IVA_Azienda='"
+ partitaivazienda + "'";
Greeting to all smart people around here !!
I have faced a weird interview question regarding SQL.
Qn . If I have 100 tables in Database. I want to fetch common records from Each table.
For example, location is common field in 100 tables. I want to fetch location field from all the tables without mentioning each table name in my SQL query.
Is there any way to do it?
If any possibilities let me know...
get list of tables from db metadata, and then query with each:
Statement stmt = conn.createStatement();
ResultSet locationRs = null;
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
locationRs = stmt.executeQuery("SELECT location from "+ rs.getString(3));
System.out.println(locationRs.getString(1));
}
In MSSQL Server you have INFORMATION_SCHEMA.COLUMNS table that contains the column names so you can use group by and having count some value you will get the column name after that you can use pivot to get the values of column names and carry on to it. You will get the ans.
For eg.
Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS group BY COLUMN_NAME having count(COLUMN_NAME) > 2
By above query you will get the common column names
You can try this for any Number of Tables in a DB :
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS group by COLUMN_NAME having count(COLUMN_NAME)=(select count(*) from INFORMATION_SCHEMA.TABLES)
My friend has found answer for my question..
To get common column from multiple tables,Use INFORMATION_SCHEMA.COLUMNS and common column name.
Query :
select *from information_schema.columns where column_name='column name'
Hope this will helpful !
I am assuming you already have connection and statemnt object's. Now try the below; it might work for you, if not make some adjustments with loops and conditions. Also, you need to have two ResultSet Objects ex: rs1 and rs2. DatabaseMetaData dbmd = con.getMetaData();
String table[] = {"TABLE"} `;
rs1 = dbmd.getTable(null, null, ".*" ,table);
while(rs1.next()){
String tableFrom = rs1.getString(3) ;
rs2 = dbmd.getColumns(null,null,tableFrom , ".*") ;
while(rs2.next()) {
String locColFrom = rs2.getString(3);
if(locColFrom .equalsIgnoreCase("location"))
stmt.executeQuery(select locColFrom from tableFrom ) ;
}
}
Here's an link to study [Oracle] (http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]))
consider a query like select a.apple,b.mango,c.orange from A a,B b,C c where ... (Some conditions)
here i need to fetch only the column names based on the query.
apple
mango
orange.
we have a query builder where end user creates/generates any type of query using that, my duty is to select only the column names from the query as above for my further operations.
How can i achieve this, through java code or query?
my db is sql server 2005.
to get the ResultSetMetaData java interface
PreparedStatement ps=con.prepareStatement("select * from your_table_name");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
con.close();
In the case you need only to display the columns name that you already know you can simply put them(column names) directly into the SELECT list :
SELECT apple AS apple ,mango AS mango,orange AS orange
otherwise you can query the information schema service table of SQL Server :
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME=OBJECT_NAME(OBJECT_ID('a')) OR TABLE_NAME=OBJECT_NAME(OBJECT_ID('b')) OR TABLE_NAME=OBJECT_NAME(OBJECT_ID('c'))
With Java and from the original query you can read the column names using ResultSetMetaData object :
ResultSetMetaData rsmd = rs.getMetaData();
String apple = rsmd.getColumnName(1); //Column apple
String mango = rsmd.getColumnName(2);
if you are using eclipseLink you could get the columns names as per this link
JPA/EclipseLink - Retrieve Columns Names
I found a solution for this and it is working fine. Thank you everyone for spending your valuable time.
We can achieve this in 2 ways.
Through Map.
ex:
List> lsMapStringObj = new ArrayList>();
for(Map myMap: lsMapStringObj)
{
for (Map.Entry entry : myMap.entrySet())
{
lsRsult.add(entry.getKey());
}
}
But here if the query returns > 0 rows then only i can fetch the column names.
This gives the solution as i expect (>= 0 rows)
PreparedStatement ps=con.prepareStatement(sQuery);
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int numColumns = rsmd.getColumnCount();
for (int i = 1; i <= numColumns; ++i)
{
lsResult.add(rsmd.getColumnName(i));
}
return lsResult;
Finally Got it, Hope it helps to others. Enjoy Programming...!!!
One of the simpler solutions is to tokenize the column names using String.split. It will return an array of columnNames.
String query = "select a.apple,b.mango,c.orange from A a,B b,C c where ... (Some conditions)";
String[] columnNames= query.split(" [fF][rR][oO][mM] ")[0]
.split("[ ]*[sS][eE][lL][eE][cC][tT] ")[1]
.split("[ ]*,[ ]*");
for(int i=0; i<columnNames.length;i++){
System.out.println(i+".)"+columnNames[i]);
}
Output:
{a.apple,b.mango,c.orange} //String array
//Sysout
1.)a.apple
2.)b.mango
3.)c.orange
Lets say i have four tables i want to read from:
customer
customer_id, customer_name
1 Joe Bolggs
customer_orders
customer_id, order_no
----------------------------
1 1
1 2
1 3
customer_addresses
customer_id address
----------------------------
1 11 waterfall road
1 23 The broadway
customer_tel_no
customer_id number
----------------------------
1 523423423432
1 234342342343
The customer information shown above (for the customer with id=1) is to be stored in a Java object as shown below
public class Customer{
String customer_id;
String customerName;
ArrayList<String> customerOrders;
ArrayList<String> customerAddress;
ArrayList<String> customerTelephoneNumbers;
}
The only way i can think of to get the above information is by using three queries. The reason is that there is a 1:* relationship between the customer table and each of the other tables. To get the data i am doing something like this:
Customer customer = new Customer()
String customerSQL = "Select * from customer where customer_id = ?";
statement = getConnection().prepareStatement(contactsQuery);
statement.setString(1,1);
resultSet = statement.executeQuery();
while (resultSet.next()){
customer.customer_id = resultSet.get(1); //No getters/setters in this example
customer.customerName = resultSet.get(2);
}
String customerOrdersSQL = "Select * from customer_orders where customer_id = ?";
statement = getConnection().prepareStatement(customerOrdersSQL);
statement.setString(1,1);
resultSet = statement.executeQuery();
customer.customerOrders = new ArrayList();
while (resultSet.next()){
customer.customerOrders.add(resultSet.getString(2); // all the order numbers
}
String customerAddressesSQL = "Select * from customer_addresses where customer_id = ?";
statement = getConnection().prepareStatement(customerAddressesSQL );
statement.setString(1,1);
resultSet = statement.executeQuery();
customer.customerAddresses = new ArrayList();
while (resultSet.next()){
customer.customerAddresses.add(resultSet.getString(2); // all the addresses
}
String customerTelSQL = "Select * from customer_tel_no where customer_id = ?";
statement = getConnection().prepareStatement(customerTelSQL);
statement.setString(1,1);
resultSet = statement.executeQuery();
customer.customerTelephoneNumbers = new ArrayList();
while (resultSet.next()){
customer.customerTelephoneNumbers.add(resultSet.getString(2); // all the order numbers
}
The problem with the above is that i am making three calls to the database. Is there a way i can merge the above into a single query please?
I cant see how a join would work because for example, a join between customer and customer_orders will return a customer row for each row in customer_orders. Is there anyway i can merge the above three queries into one?
I would think that something like this would work:
SELECT c.customer_id, c.customer_name, o.order_no, a.address, t.number
FROM customer c LEFT JOIN customer_orders o ON c.customer_id = o.customer_id
LEFT JOIN customer_addresses a ON c.customer_id = a.customer_id
LEFT JOIN customer_tel_no t ON c.customer_id = t.customer_id
WHERE c.customer_id = ?
Then, in your code, after you execute the query:
while (resultSet.next())
{
customer.customerOrders.add(resultSet.getString(3));
customer.customerAddresses.add(resultSet.getString(4));
customer.customerTelephoneNumbers.add(resultSet.getString(5));
}
Of course, this does not take into account the fact that you will have null values along the way, so I'd advise checking for nulls to make sure that you aren't adding a lot of junk to your array lists. Still, that's probably a lot less costly than 3 separate queries.
Nothing prevents you from iterating and processing the joined result into your customer object. If your application is complex enough, you could look into ORM frameworks which would do that for you under the covers. If you are working with JavaEE, have a look at JPA.
use this query and reduce the number of call. And, in while loop process on data.
select customer.customer_id,customer.customer_name,order_no,address,number
from customer,customer_orders,customer_addresses,customer_tel_no
where customer.customer_id = customer_orders.customer_id
AND
customer.customer_id = customer_addresses.customer_id
AND
customer.customer_id = customer_tel_no.customer_id