I wrote a small program to retrieve records in my email queue table to process and send email. While using JDBC to achieve this, as shown below:
MySqlConnect con=new MySqlConnect();
public PreparedStatement preparedStatement = null;
public Connection con1 = con.connect();
//pick up queue and send email
public void email() throws Exception {
try{
while(true) {
String sql = "SELECT id,user,subject,recipient,content FROM emailqueue WHERE status='Pending' ";
PreparedStatement statement = con1.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
String subject = rs.getString("subject");
String recipient = rs.getString("recipient");
String content = rs.getString("content");
String id = rs.getString("id");
String username = rs.getString("user");
String emailStatus = "DONE";
String errormsg=sendEmail(recipient, subject, content, id,username);
if (!errormsg.equals("")) {
emailStatus = "FAILED";
}
TerminalLogger.printMsg("Status : " + emailStatus);
}
statement.close();
rs.close();
}
}
}catch(Exception e){
e.printStackTrace();
TerminalLogger.printMsg("Exception: "+e.toString());
}
con1.close();
Thread.sleep(2000);
}
The above works fine since it retrieve email records where the status is pending and pass the subject, content, recipient, etc to sendemail method and done.
I wanted to achieve the same goal but using JPA persistence instead. So I wrote this method:
public Object getrecords() {
try {
String sql = "select p.id,p.user,p.subject,p.recipient,p.content from Emailqueue p where " +
"status='Pending'";
List<Object[]> resList =(List<Object[]>) em.createQuery(sql).getResultList();
if (resList == null) {
throw new Exception("Error with selection query.");
}
if (resList.size() > 0) {
return resList;
}
// msg = "Setting <" + name + "> not found.";
return null;
} catch (Exception e) {
msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
this.getClass().getName(), "get(" + "Pending" + ")", e.getMessage());
return null;
}
}
It has no issue retrieving the records and size in the table. And I called this method in the email(). Something like:
Object records = ejbCon.getSettingsFacade().getrecords();
From here, I can't figure how do I loop the records, to get each of the value in each field. For example, if there's 2 pending email records, I should need to retrieve each of its content, subject, recipient, etc and pass to sendemail method.
Also, retrieving these records is already taxing to my program since it takes long time to close the application compared to using JDBC (it's faster and I guess more efficient?).
So, I also need to consider the performance by doing using this method.
Edit
I should have clarified this better to explain what I'm trying to achieve.
So I have that email queue table in this form:
id user subject content recipient status
1 user1 test example abc#example.com Pending
2 user2 test2 example cde#example.com Pending
So before this, I was using resultset in JDBC to obtain each individual field values for id:1 and pass them to the send method, and proceed with id:2 and do the same, an iteration. Now I want to achieve the same way by using the object I retrieve but I can't specify which fields value it's able to get. So I am stuck.
There are many ways to achieve your desired result but the easiest way you can do is to create a parameterized constructor in your Emailqueue class and change your query to
String sql = "select NEW
Emailqueue(p.id,p.user,p.subject,p.recipient,p.content) from Emailqueue p
where " +
"p.status='Pending'";
List<Emailqueue> resList =(List<Emailqueue>)
em.createQuery(sql).getResultList();
this way you can normally iterate over List using foreach loop.
If i understood what is your correctly needs, Try to loop your resList.
And i recommend to you don't get Object type.
List<Emailqueue> resList = em.createQuery(sql, Emailqueue.class).getResultList();
for (Emailqueue email : resList) {
String subject = email.getSubject();
// something do.
}
Here the simple way to get data based on query.
If you are using Entity Manager for query and use HQL, u can just simply return the class data. here is the sample :
TypedQuery q = man.createQuery("SELECT u FROM YourTable u WHERE u.col1 =:col1 and u.col2 =:col2", YourCalss.class)
.setParameter("col1", col1)
.setParameter("col2", col2);
result = q.getResultList();
If you want with custom result, then u can modify the current class of entity into another class.
List<CustomClass> result = null;
EntityManager man = PersistenceUtilities.getEntityManagerFactory().createEntityManager();
try {
TypedQuery q = man.createQuery("SELECT NEW " + CustomClass.class.getCanonicalName() + "(u.col1,u.col2) "
+ "FROM YourTable as u ", CustomClass.class);
if (q.getResultList().size() > 0) {
result = q.getResultList();
}
} catch (Throwable t) {
Main.logger.error(t.getMessage());
} finally {
man.close();
}
return result;
Related
How can I query data from my database and assign it to an array of strings? In this attempt I noticed I would receive an out of bounds error before I included the resultSet.next() call since it seems that ResultSet starts at 0 and is not called like a list / array (meaning you can access the contents with its index).
public String[][] retrieveNameAndLocation() {
final String table = "customers";
try {
ResultSet resultSet = statement.executeQuery(
"SELECT " +
"first_name," +
"location" +
" FROM " + table
);
resultSet.next();
final String[] names = (String[]) (resultSet.getArray(1).getArray());
final String[] location = (String[]) (resultSet.getArray(2)).getArray();
final String[][] nameAndCountry = {names, location};
resultSet.close();
return nameAndCountry;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
Anyways the above code resulted in a SQLFeatureNotSupportedException. My next attempt was to simply call the the columns by name since I noticed it was an option inside of getArray, however that also resulted in the not supported exception.
public String[][] retrieveNameAndLocation() {
final String table = "customers";
try {
ResultSet resultSet = statement.executeQuery(
"SELECT " +
"first_name," +
"location" +
" FROM " + table
);
resultSet.next();
final String[] names = (String[]) (resultSet.getArray("first_name").getArray());
final String[] location = (String[]) (resultSet.getArray("location")).getArray();
final String[][] nameAndCountry = {names, location};
resultSet.close();
return nameAndCountry;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
I am not really sure why I need to include resultSet.next() because it seems like it's just broken since why would they include an option to query columns if they forced you to loop through the indexes?
I think you misunderstand the purpose of method getArray. Some DBMSs, like Oracle, have "array" data types. Hence the getArray method – to query a database table column whose type is an array type. I have no experience with MySQL but it appears that it does not have an array type. Hence the JDBC driver for MySQL does not need to implement the getArray method and that's why you get the SQLFeatureNotSupportedException.
You need to iterate through the ResultSet and build up your array. However since you usually don't know how many rows there are in a ResultSet, I usually use a List and then, if required, convert it to an array because when you declare an array you need to know its size.
I would also define a record and declare a List of records.
(Note that below code is not compiled and not tested since I don't have your database and I can't simulate it since the code in your question is not a minimal, reproducible example.)
public record NameAndCountry(String name, String location) {
public static java.util.List<NameAndCountry> retrieveNameAndLocation() {
final String table = "customers";
try {
ResultSet resultSet = statement.executeQuery(
"SELECT " +
"first_name," +
"location" +
" FROM " + table
);
java.util.List<NameAndCountry> list = new java.util.ArrayList<>();
while (resultSet.next()) {
String name = resultSet.getString(1);
String location = resultSet.getString(2);
NameAndCountry row = new NameAndCountry(name, location);
list.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
Context:
Local database made with sqlite3 called lims.db which contains a table called privLevels with columns user storing usernames and level storing integers from 0 to 2, but as a string.
privLevels
~~~~~~~~~~~~~~
user | level
~~~~~~~~~~~~~~
admin | 0
john | 1
kevin | 2
susan | 1
I'm trying to run and SQL SELECT query on this table and then pass the results into a Map object, with the values from user being the key, and values from level being the value.
privMap
~~~~~~~~~~~~~~~
<"admin" = "0">
<"john" = "1">
<"kevin" = "2">
<"susan" = "1">
The problem I'm having is that the ResultSet generated is returned as completely empty. The while loop in the code below never runs, and in the IntelliJ debugger, the ResultSet object is empty. In SQLiteStudio, the query runs correctly. In the rest of my code, statements run fine, but queries don't.
Code:
getPrivMapFromDatabase() should work - if the ResultSet contained anything. It gets the value from user and level and then .puts() them into the Map.
public void getPrivMapFromDatabase() {
ResultSet results = Base.getStringMapFromDatabase("privLevels", "user", "level");
try {
while(results.next()) {
String user = results.getString("user");
System.out.println(user);
String level = results.getString("level");
System.out.println(level);
privMap.put(user, level);
}
}
catch (SQLException e) {
System.out.println("[EXCEPTION] " + e.getMessage());
e.printStackTrace();
}
}
These methods are in the "Base" class.
I wrote getStringMapFromDatabase() like this because I need to do the same thing to another table and map. It should generate the string (which I checked is syntactically correct) and then execute it.
executeQuery() should just run the query and then return the ResultSet to getPrivMapFromDatabase(). However, the ResultSet object is empty. CREATE / INSERT statements work fine (using a seperate methods for running statements).
private static String url = "jdbc:sqlite:C:/Users/#My_Username#/IdeaProjects/#Project_Name#/sqlite/db/lims.db";
//Username and project name redacted
public static ResultSet getStringMapFromDatabase(String table, String keyColumn, String valueColumn) {
String sql = "SELECT " + keyColumn + ", " + valueColumn + " FROM " + table + ";";
return executeQuery(sql);
}
public static ResultSet executeQuery(String sql) {
ResultSet results = null;
try (Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement()) {
results = statement.executeQuery(sql);
}
catch (SQLException exception) {
System.out.println("[EXCEPTION] " + exception.getMessage());
}
return results;
}
Why is the ResultSet coming back empty, even though the query runs fine in SQLiteStudio and other statements work? The table name, and column names are correct.
I have read a similar post but I still cannot get what is the problem.
I created a table in ms access, named DOCTOR, there are columns: DoctorID(number), Name(text), PhoneNumber(number), Department(text) and Specialization(text)
I connect the database to java through UCanAccess, below is the code to get connection
import java.sql.*;
public class Doctor
{
public static Connection connection; //sharing the memory
public static Connection connect() throws ClassNotFoundException, SQLException
{
String db = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(db);
String url = "jdbc:ucanaccess://C:/Users/user.oemuser/Documents/Doctor.accdb";
connection = DriverManager.getConnection(url);
return connection;
}
}
In my GUI class, i have a method called getConnect to show the data from database to textfield
public void getConnect()
{
try
{
connection = Doctor.connect();
statement=connection.createStatement();
String sql = "SELECT * FROM DOCTOR";
results = statement.executeQuery(sql);
results.next();
id = results.getInt("DoctorID");
name = results.getString("DoctorName");
phone = results.getInt("PhoneNumber");
dept = results.getString("Department");
spec = results.getString("Specialization");
textField1.setText("" +id);
textField2.setText(name);
textField3.setText("" +nf3.format(phone));
textField4.setText(dept);
textField5.setText(spec);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
and below is the code for the button1 which is the next button.
if(evt.getSource() == button1)
{
try
{
connection = Doctor.connect();
connection.setAutoCommit(false);
statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql1 = "SELECT * FROM DOCTOR";
results = statement.executeQuery(sql1);
if(results.next())
{
textField1.setText("" +results.getInt("DoctorID"));
textField2.setText(results.getString("DoctorName"));
textField3.setText("" +nf3.format(results.getInt("PhoneNumber")));
textField4.setText(results.getString("Department"));
textField5.setText(results.getString("Specialization"));
}
else
{
results.previous();
JOptionPane.showMessageDialog(null, "No more records");
}
connection.commit();
}
catch(Exception ex){
ex.printStackTrace();
}
}
Obviously the best component to use here is a JTable if you want to query all records within a particular database table or at the very least place the result set into an ArrayList mind you database tables can hold millions+ of records so memory consumption may be a concern. Now, I'm not saying that your specific table holds that much data (that's a lot of Doctors) but other tables might.
You can of course do what you're doing and display one record at a time but then you should really be querying your database for the same, one specific record at a time. You do this by modifying your SQL SELECT statement with the addition of the WHERE clause statement and playing off the ID for each database table record, something like this:
String sql = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";
But then again we need to keep in mind that, if the schema for your DoctorID field is set as Auto Indexed which of course allows the database to automatically place a incrementing numerical ID value into this field, the Index may not necessarily be in a uniform sequential order such as:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,.......
instead it could possibly be in this order:
1, 3, 4, 5, 6, 9, 10, 11, 16, 17,....
This sort of thing happens in MS Access Tables where a table record has been deleted. You would think that the ID slot that is deleted would be available to the next record added to the table and would therefore hold that removed ID value but that is not the case. The Auto Index Increment (autonumber) simply continues to supply increasing incremental values. There are of course ways to fix this sequencing mismatch but they are never a good idea and should truly be avoided since doing so can really mess up table relationships and other things within the database. Bottom line, before experimenting with your database always make a Backup of that database first.
So, to utilize a WHERE clause to play against valid record ID's we need to do something like this with our forward and reverse navigation buttons:
Your Forward (Next) Navigation Button:
if(evt.getSource() == nextButton) {
try {
connection = Doctor.connect();
connection.setAutoCommit(false);
number++;
long max = 0, min = 0;
ResultSet results;
Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Get the minimum DoctorID value within the DOCTOR table.
String sql0 = "SELECT MIN(DoctorID) AS LowestID from DOCTOR";
results = statement.executeQuery(sql0);
while (results.next()){ min = results.getLong("LowestID"); }
// Get the maximum DoctorID value within the DOCTOR table.
sql0 = "SELECT MAX(DoctorID) AS HighestID from DOCTOR";
results = statement.executeQuery(sql0);
while (results.next()){ max = results.getLong("HighestID"); }
if (max <= 0) {
JOptionPane.showMessageDialog(null, "No records found in Doctor Table.");
return;
}
if (number > min) { previousButton.setEnabled(true); }
if (number > max) {
nextButton.setEnabled(false);
JOptionPane.showMessageDialog(null, "No more records");
number--;
}
results = null;
while (results == null) {
String sql1 = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";
results = statement.executeQuery(sql1);
long id = 0;
// Fill The GUI Form Fields....
while (results.next()){
//id = results.getLong("DoctorID");
textField1.setText("" +results.getInt("DoctorID"));
textField2.setText(results.getString("DoctorName"));
textField3.setText("" + results.getString("PhoneNumber"));
textField4.setText(results.getString("Department"));
textField5.setText(results.getString("Specialization"));
connection.commit();
return;
}
// ----------------------------------------------------------
if (id != number) { results = null; number++; }
}
}
catch(Exception ex){ ex.printStackTrace(); }
}
Your Reverse (Previous) Navigation Button:
if(evt.getSource() == previousButton) {
try {
connection = Doctor.connect();
connection.setAutoCommit(false);
number--;
long max = 0, min = 0;
ResultSet results;
Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Get the minimum DoctorID value within the DOCTOR table.
String sql0 = "SELECT MIN(DoctorID) AS LowestID from DOCTOR";
results = statement.executeQuery(sql0);
while (results.next()){ min = results.getLong("LowestID"); }
// --------------------------------------------------------------------------
// Get the maximum DoctorID value within the DOCTOR table.
sql0 = "SELECT MAX(DoctorID) AS HighestID from DOCTOR";
results = statement.executeQuery(sql0);
while (results.next()){ max = results.getLong("HighestID"); }
// --------------------------------------------------------------------------
if (max <= 0) {
JOptionPane.showMessageDialog(null, "No records found in Doctor Table.");
return;
}
if (number < min) {
previousButton.setEnabled(false);
JOptionPane.showMessageDialog(null, "No more records");
number++;
}
if (number < max) { nextButton.setEnabled(true); }
results = null;
while (results == null) {
String sql1 = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";
results = statement.executeQuery(sql1);
long id = 0;
// Fill The GUI Form Fields....
while (results.next()){
textField1.setText("" +results.getInt("DoctorID"));
textField2.setText(results.getString("DoctorName"));
textField3.setText("" + results.getString("PhoneNumber"));
textField4.setText(results.getString("Department"));
textField5.setText(results.getString("Specialization"));
connection.commit();
return;
}
// ----------------------------------------------------------
if (id != number) { results = null; number--; }
}
}
catch(Exception ex){ ex.printStackTrace(); }
}
Things To DO...
So as to remove duplicate code, create a method named
getMinID() that returns a Long Integer data type. Allow this method to accept two String Arguments (fieldName and
tableName). Work the above code section used to gather the minimum DoctorID value within the DOCTOR table into the new
**getMinID() method. Use this new method to replace the formentioned code for both the Forward (Next) and Revese (Previous)
buttons.
So as to remove duplicate code, create a method named
getMaxID() that returns a Long Integer data type. Allow this method to accept two String Arguments (fieldName and
tableName). Work the above code section used to gather the maximum DoctorID value within the DOCTOR table into the new
getMaxID() method. Use this new method to replace the formentioned code for both the Forward (Next) and Revese (Previous)
buttons.
So as to remove duplicate code, create a void method named
fillFormFields(). Allow this method to accept two arguments, one as Connection (*connection) and another as ResultSet
(results) . Work the above code section used to Fill The GUI
Form Fields into the new fillFormFields() method. Use this new
method to replace the formentioned code for both the Forward (Next)
and Revese (Previous) buttons.
Things To Read That Might Be Helpful:
The SQL WHERE clause statement and the SQL ORDER BY statement for sorting your result set.
Searching For Records
I would like to see only that products user is looking for them, but when second if is executed it will push(pointer or whatever is there) to next ID(id I have as unique so it will push to nowhere) and result is null. I hope you understand my problem :).
if (stmt.execute(
"SELECT * FROM products where ID=" + removeName)) {
rs = stmt.getResultSet();
if (!rs.next()) {
m = "ID not found.";
return m;
}
In your case, you can go for PreparedStatement for avoiding SQL-Injection problem.
PreparedStatement prodsQuery= con.prepareStatement("SELECT * FROM products where ID=?");
prodsQuery.setInt(1,removeName);
ResultSet rs = prodsQuery.executeQuery();
if(!rs.next())
{
m = "ID not found.";
return m;
}
The problem is that you're reading the first result in order to know if there's at least one result, then trying to consume the next results and missing the first one (adapted from your question description). I gave an explanation of how this works here.
A possible solution for this problem would be assuming the query executed with no problems and you have your results, then retrieve the data (or List of data) and as a last step verify if the data is not null or the List of data is not empty.
Code adapted from Naveen's answer to show the proposed solution
PreparedStatement prodsQuery =
con.prepareStatement("SELECT * FROM products where ID=?");
prodsQuery.setInt(1,removeName);
ResultSet rs = prodsQuery.executeQuery();
Assuming there's only one result to get:
//also assuming you will set the results in a Data class (yes, this can be replaced)
Data data = null;
if (rs.next()) {
//logic to retrieve data...
data = new Data();
data.setSomething(rs.get(1));
//more and more code to fill the data...
//because it looks that you need it as String (wonder why you return a String as well)
return data.toString();
}
//note: I use an else statement to check if indeed there were no results at all
//else statement added using a line separator for code explanation purposes
else {
m = "ID not found.";
return m;
}
Assuming there is a list of results to get:
//also assuming you will set the results in a Data class (yes, this can be replaced)
List<Data> dataList = new ArrayList<Data>();
while (rs.next()) {
//logic to retrieve data...
Data data = new Data();
data.setSomething(rs.get(1));
//more and more code to fill the data...
//because it looks that you need it as String (wonder why you return a String as well)
dataList.add(data);
}
//in this case, there's no validation in order to know if there's any result
//the validation must be in the client of this class and method checking if
//the result list is empty using if(!List#isEmpty) { some logic... }
return dataList;
First thing, your approach is vulnerable to SQL Injection. Please go for PreparedStatement.
Look at this simple example for using PreparedStatement
And you should do like this :
ResultSet rs = stmt.executeQuery("SELECT * FROM products where ID=" + removeName);
if (!rs.next()) {
m = "ID not found.";
return m;
}
I've started creating a toDoList and I like to create a "DataMapper" to fire queries to my Database.
I created this Datamapper to handle things for me but I don't know if my way of thinking is correct in this case. In my Datamapper I have created only 1 method that has to execute the queries and several methods that know what query to fire (to minimalize the open and close methods).
For example I have this:
public Object insertItem(String value) {
this.value = value;
String insertQuery = "INSERT INTO toDoList(item,datum) " + "VALUES ('" + value + "', CURDATE())";
return this.executeQuery(insertQuery);
}
public Object removeItem(int id) {
this.itemId = id;
String deleteQuery = "DELETE FROM test WHERE id ='" + itemId + "'";
return this.executeQuery(deleteQuery);
}
private ResultSet executeQuery(String query) {
this.query = query;
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = db.connectToAndQueryDatabase(database, user, password);
st = con.createStatement();
st.executeUpdate(query);
}
catch (SQLException e1) {
e1.printStackTrace();
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e2) { /* ignored */}
}
if (st != null) {
try {
st.close();
} catch (SQLException e2) { /* ignored */}
}
if (con != null) {
try {
con.close();
} catch (SQLException e2) { /* ignored */}
}
System.out.println("connection closed");
}
return rs;
}
So now I don't know if it's correct to return a ResultSet like this. I tought of doing something like
public ArrayList<ToDoListModel> getModel() {
return null;
}
To insert every record returned in a ArrayList. But I feel like I'm stuck a little bit. Can someone lead me to a right way with an example or something?
It depends on the way the application works. If you have a lot of databases hits in a short time it would be better to bundle them and use the same database connection for all querys to reduce the overhead of the connection establishment and cleaning.
If you only have single querys in lager intervals you could do it this way.
You should also consider if you want to seperate the database layer and the user interface (if existing).
In this case you should not pass the ResultSet up to the user interface but wrap the data in an independent container and pass this through your application.
If I understand your problem correctly!, you need to pass a list of ToDoListModel objects
to insert into the DB using the insertItem method.
How you pass your object to insert items does not actually matter, but what you need to consider is how concurrent this DataMapper works, if it can be accessed by multiple threads at a time, you will end up creating multiple db connections which is little expensive.Your code actually works without any issue in sequential access.
So you can add a synchronized block to connection creation and make DataMapper class singleton.
Ok in that case what you can do is, create a ArrayList of hashmap first. which contains Key, Value as Column name and Column value. After that you can create your model.
public List convertResultSetToArrayList(ResultSet rs) throws SQLException{
ResultSetMetaData mdata = rs.getMetaData();
int columns = mdata.getColumnCount();
ArrayList list = new ArrayList();
while (rs.next()){
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(md.getColumnName(i),rs.getObject(i));
}
list.add(row);
}
return list;
}