sqlite query using java - java

I'm working on shifts manager program which calculates monthly salary and etc.
the program based on SQLite database which keeps getting updated by the user input.
my question is , how can i use the SQLite function in java to retrieve information, lets say monthly salary in one command (i know i can use " select sum(tips) between date1 and date2",but how can i get the function result inside a variable?)
so far i've created a function which gets two dates and retrieves all the shifts salary between these dates and summarise them with ResultSet.
here's my code:
public static String tipsMade(String date1, String date2){
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
String ans= null;
int sum = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gil\\test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
rs = stmt.executeQuery("select tips from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum += rs.getInt("tips");
}
ans = Integer.toString(sum);
//
//close connections and etc
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
return ans;
}

I edited your code.
Note that in case you select the sum of the tips, the column name changes. You also get only one row with one column as your result, so you should not need the while loop anymore.
The sum of the tips is now saved in the variable sum
public static String tipsMade(String date1, String date2){
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
String ans= null;
int sum = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gil\\test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
rs = stmt.executeQuery("select sum(tips) from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum = rs.getInt("sum(tips)");
}
ans = Integer.toString(sum);
//
//close connections and etc
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
return ans;
}

Related

Exhausted ResultSet Issue with Servlet Oracle

Every time I run this code it gives me an exhauset resultset error. Im not sure what Im doing wrong but Ive tried removing the .next(); code for either one or all resultsets and then the error given is the ResultSet next wasnt called.
Im not sure what Im doing wrong. Just curious what people might think the issue could be? Ive done similar earlier in my servlet code but only used 1 statement and then 1 prepared statement. This time Im using 2 statements and 1 prepared statement.
String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form
stmt = con.createStatement();
stmt1 = con.createStatement();
ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table
rs11.next();
ResultSet rs12 = stmt.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
rs12.next();
int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
int olo1 = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
PreparedStatement pstmt1 = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)");
pstmt1.clearParameters();
pstmt1.setInt(1,olo);
pstmt1.setInt(2,olo1);
ResultSet rs1 = pstmt1.executeQuery();
rs1.next();
Some ideas on your code (in comments)
stmt = con.createStatement();
stmt1 = con.createStatement();
ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table
//Check if you HAVE a line here!
if(!rs11.next()) {
System.out.println("No Recipe Found");
}
//Use stmt1 - that's why you created it?!
ResultSet rs12 = stmt1.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
if(!rs12.next()) {
System.out.println("No Ingredient Found");
}
int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
//Read Ingredient from rs12 -> that's where you selected it into
int olo1 = ((Number) rs12.getObject(1).intValue(); //convert resultset value to int
While this might point you into the right direction for solving the current issue, you should consider learning about clean code.
Consider this code making use of try-with-resource, refactored out some methods, using prepared statements.
//Replace exiting code
String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form
int recipieId = getRecipeId(con, opt1);
int ingredientId = getIngredientId(con, ingr1);
if(recipeId > 0 && ingredientId > 0) {
//Process result
insertRecLnk(con, recipeId, ingredientId);
} else {
System.out.println("No INSERT");
}
//Helper functions
protected int getRecipeId(Connection con, String rec) {
try(PreparedStatement st = con.prepareStatement("SELECT recipe_ID FROM GM_Recipes WHERE rec_name=?")) {
st.setString(1, rec);
try(ResultSet rs11 = st.executeQuery()) {
//choose recipe_ID from sql table
if(rs11.next()) {
return rs11.getInt(1);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
System.out.println("No Recipe Found");
return -1;
}
protected int getIngredientId(Connection con, String ing) {
try(PreparedStatement st = con.prepareStatement("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name=?")) {
st.setString(1, ing);
try(ResultSet rs11 = st.executeQuery()) {
//choose recipe_ID from sql table
if(rs11.next()) {
return rs11.getInt(1);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
System.out.println("No Ingredient Found");
return -1;
}
protected void insertRecLnk(Connection con, int rId, int iId) {
try(PreparedStatement ps = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)")) {
ps.setInt(1, rId);
ps.setInt(2, iId);
ps.executeUpdate();
} catch(SQLException e) {
e.printStackTrace();
}
}

to check whether the search element isn't available in DB

I'm using phpmy admin and I need to Display "Not Found" message in case searching element is not found in the DB.
Used code is here.
Connection c = DBconnect.connect();
Statement s = c.createStatement();
String e = txtempId.getText();
ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");
I used this method to search empId ,if empId is not available in db I need to display a message.Please give me a solution how to detect, if empId is not available in DB.
if (rs != null)
{
out.println("result set has got something");
while (rs.next())
{
//I am processing result set now
}
}
else
{
out.println("Not Found");
}
Use if statement like this
Connection c = DBconnect.connect();
Statement s = c.createStatement();
String e = txtempId.getText();
ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");
if(rs.next())
{
do
{
// If there is data, then process it
}
while(rs.next());
}
else
System.out.println("Not Found");
Added parse of text to integer, assuming empId is an integer.
int empId = Integer.parseInt(txtempId.getText());
try (Connection c = DBconnect.connect()) {
String sql = "SELECT *" +
" FROM nonacademic" +
" WHERE empId = ?";
try (Statement s = c.prepareStatement(sql)) {
s.setInt(1, empId);
try (ResultSet rs = s.executeQuery()) {
if (! rs.next()) {
// not found
} else {
// found, call rs.getXxx(...) to get values
}
}
}
}
Just use the basic simple if & else statement. If the ResultSet is "null" or it doesn't contain any record display the Message otherwise read data & display.
Connection c = DBconnect.connect();
Statement s = c.createStatement();
String e = txtempId.getText();
ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");
if(rs.next())
// record found do the processing
else
System.out.println("Not Found");
String e = txtempId.getText();
String sql="select *from nonacademic where empId='"+ e+"' ";
try {
boolean status=DatabaseConnection.checkValue(sql);
if (status) {
JOptionPane.showMessageDialog(null,
"This id is available");
} else {
JOptionPane.showMessageDialog(null,
"Not found");
}
} catch (Exception e) {
}
This method return check whether the search element is exist or not
public static boolean checkValue(String sql) throws Exception {
boolean b = false;
ResultSet rst = null;
Statement st = getStatement();
rst = st.executeQuery(sql);
if (rst.next()) {
b = true;
}
return b;
}

Show table names in MySQL database and the count of their records

I'm working on the next step of the class assignment and I want to do a query where I pull the metadata from the database to list the tables in it then include the count of records in the tables. I have it where it kind of works but in the first half shown it only puts a number 1 where the count of records should go. In the second half (I'm doing this for testing purposes now and will only have the first half if I can get it to work) it prints out the number of records just fine but I have the table name hard coded in. I want it to be able to read in the table name from the metadata result then execute the query for the count of records. I am running this in netbeans with a MySQL database and am using the JDBC driver.
connect();
try
{
String db = conn.getCatalog();
data.append("The current database " + db + " contains the following tables:\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet rs = null;
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next())
{
String tbl = rs.getString(3);
System.out.print(tbl + "\r\n");
ResultSet ct = null;
ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM" + tbl);
while (ct.next())
{
System.out.print(ct.getString(1) + "\r\n");
data.append("Table \"" + rs.getString(3) + "\" contains " + ct.getString(1) + " records.\r\n");
}
}
data.append("\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet ct = null;
ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM customer");
while (ct.next())
{
data.append(ct.getString(1) + "\r\n");
}
ResultSet pt = null;
pt = conn.createStatement().executeQuery("SELECT COUNT(*) FROM product");
while (pt.next())
{
data.append(pt.getString(1) + "\r\n");
}
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
With following query you can query all tables with number of rows, date size, index size etc on a specific database.
SELECT concat(table_schema,'.',table_name) tables,
concat(round(table_rows),'') rows,
concat(round(data_length/(1024),2),'') data_size,
concat(round(index_length/(1024),2),'') index_size,
concat(round((data_length+index_length)/(1024),2),'') total_size,
round(index_length/data_length,2) index_data_ratio
FROM information_schema.TABLES
WHERE table_schema like "%YOUR-DATABASE-NAME-HERE%"
ORDER BY rows DESC LIMIT 20;
This is what I ended up doing and it works exactly as I wanted. I ended up using a prepared statement in the while loop to make it work.
connect();
try
{
String db = conn.getCatalog();
data.append("The current database " + db + " contains the following tables:\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet rs = null;
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next())
{
String tbl = rs.getString(3);
String query = ("SELECT COUNT(*) FROM " + tbl);
PreparedStatement cnt = conn.prepareStatement(query);
ResultSet ct = cnt.executeQuery();
while (ct.next())
{
data.append("Table " + rs.getString(3) + " has " + ct.getString(1) + " records\r\n");
}
}
data.append("\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}

java.sql.SQLException: ResultSet closed [duplicate]

This question already has answers here:
When is ResultSet closed?
(3 answers)
Closed 2 years ago.
I cant not do the further process because I get ResultSet closed while running this code. I am adding the value after 15 row of the sqlite database table and I want to find average value of 15 row and that should be store in ArrayList.
Here is the code:-
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully3");
stmt = c.createStatement();
rs = stmt
.executeQuery("SELECT TIME,BID,ASK FROM '" + title + "' ;");
System.out.println("Opened database successfully 20");
while (rs.next()) {
i++;
System.out.println(i);
if (i > 15) {
System.out.println("i am in");
List<String> stocklist = new ArrayList<String>();
String main = rs.getString(1);
System.out.println(rs.getString(1));
String s[] = main.split(" ", 2);
String date = s[0];
String time = s[1];
stocklist.add(date);
stocklist.add(time);
stocklist.add(df.format(rs.getFloat("BID")));
stocklist.add(df.format(rs.getFloat("ASK")));
rs1 = stmt
.executeQuery("SELECT ASK FROM '" + title + "' ;");
int j = 1;
while (rs1.next()) {
if (j < i) {
System.out.println(rs1.getFloat("ASK"));
avg = avg + rs1.getFloat("ASK");
}
j++;
}
rs1.close();
System.out.println("i am out");
avg = avg / 15;
changepercent = ((rs.getFloat("ASK") - avg) / avg) * 100;
stocklist.add(df.format(changepercent));
stocklist.add("ETE");
stocklist.add("HUN");
stocklist.add("ALU");
stocklist.add("ETE");
stocklist.add("HUN");
stocklist.add("ALU");
stocklist.add("ETE");
stocklist.add("HUN");
hntm.addRow(stocklist);
}
}
rs.close();
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
you should not reuse a Statement. When you create a new Query, you need so use a new Statement Object. Replace rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;"); with rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");
This is not the answer for this question, but i was also having problem with java.sql.SQLException: ResultSet closed.
In my case the result from the query was empty and i was using SQLite, so the ResultSet returned closed.
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
// resultSet closed == didn't find anything
if(resultSet.isClosed())
return false;
I tested using PostgreSQL and ResultSet stay open even when query is empty. I didn't expect different behavior from different databases.
If anyone want to check my test: https://gist.github.com/thiagola922/fcd80f0f29b83106304c3b64cf64b2b1
As #marquis-of-lorne said, a better way to check if the connection still open is using resultSet.next().

How to dump resultset data into a database table

I am creating a resultset through an SQL query ('SELECT_PROCESS_INITIAL_REQUEST') which basically just grabs everything out of an SQL database table.
Statement stmt = conn.createStatement();
String sql = SQLDataAdaptor.SELECT_PROCESS_INITIAL_REQUEST;
ResultSet rsRequest = stmt.executeQuery(sql);
I then want to dump that data into an identical table to the original but in a different database. How would I do that?
Dumping it in another database means that Java will have to handle your resultset. So i'm afraid you have to do this programmatically.
Think of "prepared statements", "add batch method" and don't forget to execute your inserts every n records.
Using the example on Java Tutorial Website-
public static void viewTable(Connection con, String dbName)
throws SQLException {
//Modify this code according to 2nd database vendor and credentials-
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn2 = DriverManager.getConnection(url);
Statement stmt = null;
Statement insertStmt = null;
String query = "select COF_NAME, SUP_ID, PRICE, " +
"SALES, TOTAL " +
"from " + dbName + ".COFFEES";
try {
stmt = con.createStatement();
insertStmt = conn2.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
String insertQuery = "INSERT INTO COFFEES (COF_NAME, SUP_ID, PRICE, sales, total) VALUES (coffeeName,supplierID, proce, sales, total);
try{
insertStmt.execute(insertQuery);
}catch(SQLExeption e){
e.printStackTrace();
}
}
} catch (SQLException e ) {
e.printStackTrace();
} finally {
if (stmt != null) { stmt.close();}
if(insertStmt != null) {insertStmt.close();}
}
}

Categories