Autonumber reset per day with Java Using Database - java

I build some software for my mini shop, I really confused with number queue customer who shop in my place. Please someone could help me.
I have a method which content with generate new number of queue from my customer. But when I open my Apps in the next day, I hope the queue is reset into 1 again.
My sytax Java like =
public void acak() {
try {
String generate = "SELECT COALESCE (MAX(no_antrian),0) AS kode from transaksi where tg_transaksi='" + tanggal + "'";
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery(generate);
if (res.next()) {
try {
String kd_barang = res.getString("kode").substring(1);
String AN = "" + (Integer.parseInt(kd_barang) + 1);
String Nol = "";
if (AN.length() == 1) {
Nol = "000";
} else if (AN.length() == 2) {
Nol = "00";
} else if (AN.length() == 3) {
Nol = "0";
} else if (AN.length() == 4) {
Nol = "";
}
lblnoantrian.setText(Nol + AN);
} catch (NumberFormatException ex) {
System.out.println(ex.getMessage());
}
} else {
lblnoantrian.setText("0001");
}
} catch (Exception e) {
e.printStackTrace();
}
}
When I run this program in the next day, I see the eror like :
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
this eror's refers to:
String AN = "" + (Integer.parseInt(kd_barang) + 1);
maybe anyone can helping me..

this is not the proper use of COALESCE
as its description says
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
in case there is a empty string so its output would be empty string check
SELECT COALESCE (MAX(NULL),0)
->0
SELECT COALESCE (MAX(''),0)
->
SELECT COALESCE ('',0)
->
as you can see the last two queries returns empty string
replace your query with this query i hope this will work
SELECT
CASE
WHEN COALESCE (MAX(no_antrian), 0) = ''
THEN 0
ELSE COALESCE(MAX(no_antrian), 0)
END AS kodec
FROM
transaksi
WHERE tg_transaksi = '" + tanggal + "' "

Related

SQL executeUpdate() seems to commit data, but that is not the case. How can I find my error?

I curently work on few SQL queries (MSSQL 2O14), but only "SELECT" query works with executeQuery().
I had use execute() and executeUpdate() on "INSERT INTO" and "UPDATE" queries, but whereas it looks like working, no way.
FYI, in "UPDATE_PREVIOUS_H_LOT_STATUT,
int count= p.executeUpdate(); return 1. If h_lot_number is an unknown lot number, count = 0.
So, if I use wrong data in input, my query isn't executed(Until here, I agree) but when I use the expected data, the query is executed but there is no change in my DB.
How can I find where my error is ?
UPDATE Function :
public static boolean UPDATE_PREVIOUS_H_LOT_STATUT(String h_lot_number_old) {
try {
setUpConnexion("mainDB");
String baseQuery = "UPDATE valve_assembly_h SET statut = 'Expiré' WHERE h_lot_number = '" + h_lot_number_old + "'";
//PreparedStatement p = newTransact("UPDATE valve_assembly_h SET statut = 'Expiré' WHERE h_lot_number = '" + h_lot_number_old + "'", "mainDB");
PreparedStatement toReturn = (PreparedStatement) mainCon.prepareStatement(baseQuery);
int count = toReturn.executeUpdate();
if (count > 0) {
Log.d("Sucess : ", "Previous h_lot updated.");
closeCons();
return true;
} else {
Log.e("Error : ", "No lot found with this number.");
closeCons();
return false;
}
} catch (SQLException e) {
error = e.getMessage();
Log.e("Error :", error);
closeCons();
return false;
}
}
LOAD PREVIOUS NUMBER FUNCTION (works perfectly)
public static String LOAD_PREVIOUS_H_LOT_NUMBER(String machineNumber) {
String s = "";
try {
setUpConnexion("mainDB");
ResultSet RS = executeQuery("SELECT h_lot_number FROM valve_assembly_h WHERE machine_number = '" + machineNumber + "' AND statut = 'Actif'", "mainDB");
while (RS.next()) {
s = RS.getString(1);
Log.d("Success : ", "Lot number : " + s);
}
closeResultSet(RS);
} catch (Exception e) {
error = e.getMessage();
Log.e("Error :", error);
s = error;
}
closeCons();
return s;
}
Set up connection function : (works perfectly)
public static boolean setUpConnexion(String DBNAME) {
StrictMode.ThreadPolicy policy;
policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String connectURL;
try {
CONNECTION MS SQL SERVER
}
return true;
} catch (Exception e) {
System.out.println("SQL ERROR: " + e.getMessage());
e.printStackTrace();
return false;
}
}
Try to commit the transaction manually through the Connection object ,hope it helps.

Trying to read 700k+ of data and the Error "GC Overhead Limit Exceeded" occurred

Alright so I need help in reviewing my codes because I'm kinda still new in programming (currently in my second year of Diploma in Computer Science). I got this error as in the title GC Overhead Limit Exceeded when I tried running my code below.
A brief explanation of this code, I'm trying to read data from a CSV File and then transfer it to a database. FYI, there are actually 10 tables/CSV files that I need to read, but on this I'll show this one table Tickets because the error only occurred when I tried to read that table/file. The other tables have hundreds of rows/data only while the table Tickets have 735,504 of rows/data. Furthermore, I've succeeded in reading 450,028 of data after 6 hours of running the code before the error occurred.
What can I do to fix this error? What can be modified to improve my code? I really appreciate it if you guys can help me :)
public class Demo2 {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/database";
String username = "root";
String password = "password";
try {
//Connect to the database
Connection connection = DriverManager.getConnection(url, username, password);
//Test on one table only
String tableName = "Tickets";
System.out.println("Connecting to TABLE " +tableName +"...");
readCSVFile(tableName, connection);
System.out.println();
System.out.println("THE END");
connection.close();//close connection to the database
}
catch (SQLException e) {
System.out.println("ERROR at main(): SQLException!!");
e.printStackTrace();
}
}
static int countNewRow = 0;
static int countUpdatedRow = 0;
//Method to read the CSV File
static void readCSVFile(String tableName, Connection conn) {
//Read CSV File
try {
String path = tableName +".csv";
BufferedReader br = new BufferedReader(new FileReader(path));
br.readLine();//skip the first line
String inData;
//Read The Remaining Line
while((inData=br.readLine()) != null)
{
String[] rowData = inData.split(",");
ArrayList <String> rowDataList = new ArrayList<String>();
for (int i=0; i<rowData.length; i++)
rowDataList.add(rowData[i]);
//To combine String that starts and ends with "
for(int i=0; i<rowDataList.size(); i++) {
if (rowDataList.get(i).charAt(0) == '"') {
String string1 = rowDataList.get(i).substring(1, rowDataList.get(i).length());
String string2 = rowDataList.get(i+1).substring(0, rowDataList.get(i+1).length()-1);
String combined = string1 +"," +string2;
rowDataList.set(i, combined);
rowDataList.remove(i+1);
break;
}
}
//Remove the RM
for(int i=0; i<rowDataList.size(); i++) {
if (rowDataList.get(i).startsWith("RM")) {
String string = rowDataList.get(i).substring(2);
rowDataList.set(i, string);
}
}
//This is just to keep track of the data that has been read
System.out.println("[" +rowDataList.get(0) +"]");
//Transfer the data to the database
insertToDatabase(conn, tableName, rowDataList);
}
System.out.println("New Row Added : " +countNewRow);
System.out.println("Updated Row : " +countUpdatedRow);
System.out.println("== Process Completed ==");
br.close();
}
catch (FileNotFoundException e) {
System.out.println("ERROR at readCSVFile(): FileNotFoundException!!");
e.printStackTrace();
}
catch (IOException e) {
System.out.println("ERROR at readCSVFile(): IOException!!");
e.printStackTrace();
}
catch (SQLException e) {
System.out.println("ERROR at readCSVFile(): SQLException!!");
e.printStackTrace();
}
catch (ParseException e) {
System.out.println("ERROR at readCSVFile(): ParseException!!");
e.printStackTrace();
}
}
static void insertToDatabase(Connection connection, String tableName, ArrayList <String> rowDataList) throws SQLException, ParseException {
String tableIdName = tableName;
if (tableIdName.charAt(tableIdName.length()-1) == 's')
tableIdName = tableIdName.substring(0, tableIdName.length()-1);
//To read row
String rowID = rowDataList.get(0);
String selectSQL = "SELECT * FROM " +tableName +" "
+"WHERE " +tableIdName +"_ID = " +rowID;
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(selectSQL);
boolean value = result.next();
//INSERT # UPDATE row
if (value == true) { //Update Row if the data is already existed
updateStatementt(tableName, connection, rowDataList);
countUpdatedRow++;
}
else { //Insert New Row
insertStatementt(tableName, connection, rowDataList);
countNewRow++;
}
}
//Method to insert data to the database
static void insertStatementt(String tableType, Connection conn, ArrayList <String> rowDataList) throws SQLException, ParseException {
//Generate Question Mark
String generateQuestionMark = null;
if(rowDataList.size() == 1)
generateQuestionMark = "?";
else
generateQuestionMark = "?, ";
for(int i=1; i<rowDataList.size(); i++) {
if(i!=rowDataList.size()-1)
generateQuestionMark += "?, ";
else
generateQuestionMark += "?";
}
//Insert sql
String sql = "INSERT INTO " +tableType +" VALUES (" +generateQuestionMark +")";
PreparedStatement insertStatement = conn.prepareStatement(sql);
//Insert data
//There are other 'if' and 'else if' statements here for other tables
else if (tableType.equals("Tickets")) {
int ticketID = Integer.parseInt(rowDataList.get(0));
int movieId = Integer.parseInt(rowDataList.get(1));
int theaterId = Integer.parseInt(rowDataList.get(2));
String[] date = rowDataList.get(3).split("/");
String dateString = date[2] +"-" +date[1] +"-" +date[0];
Date showDate = Date.valueOf(dateString);
int showTimeId = Integer.parseInt(rowDataList.get(4));
int cptId = Integer.parseInt(rowDataList.get(5));
int pcId = Integer.parseInt(rowDataList.get(6));
float amountPaid = Float.parseFloat(rowDataList.get(7));
int year = Integer.parseInt(rowDataList.get(8));
String month = rowDataList.get(9);
insertStatement.setInt(1, ticketID);
insertStatement.setInt(2, movieId);
insertStatement.setInt(3, theaterId);
insertStatement.setDate(4, showDate);
insertStatement.setInt(5, showTimeId);
insertStatement.setInt(6, cptId);
insertStatement.setInt(7, pcId);
insertStatement.setFloat(8, amountPaid);
insertStatement.setInt(9, year);
insertStatement.setString(10, month);
}
insertStatement.executeUpdate();
insertStatement.close();
}
//Method to update the data from the database
static void updateStatementt(String tableType, Connection conn, ArrayList <String> rowDataList) throws SQLException {
Statement statement = conn.createStatement();
String sql = "UPDATE " +tableType;
//There are other 'if' and 'else if' statements here for other tables
else if (tableType.equals("Tickets")) {
String[] date = rowDataList.get(3).split("/");
String dateString = date[2] +"-" +date[1] +"-" +date[0];
sql += " SET movie_id = " +rowDataList.get(1) +","
+ " theater_id = " +rowDataList.get(2) +","
+ " showdate = \"" +dateString +"\","
+ " showtime_id = " +rowDataList.get(4) +","
+ " costperticket_id = " +rowDataList.get(5) +","
+ " personcategory_id = " +rowDataList.get(6) +","
+ " amount_paid = " +rowDataList.get(7) +","
+ " year = " +rowDataList.get(8) +","
+ " month = \"" +rowDataList.get(9) +"\""
+ " WHERE ticket_id = " +rowDataList.get(0);
}
statement.executeUpdate(sql);
}
}
For short, read a single line and do whatever you want to do with it. You don't have enough memory for all 700k lines.
You should add statement.close() for the update Statement.
If you really want to read all this data into the Java heap, increase the heap size using, for example, the -Xmx command-line switch. Because of the way textual data is encoded in the JVM, you'll probably need much more heap that the total data size would suggest.
In addition, there might be some places in your code where you can take the strain off the JVM's memory management system. For example, concatenating strings using "+" can generate a lot of temporary data, which will increase the load on the garbage collector. Assembling strings using a StringBuilder might be a simple, less resource-hungry, alternative.

java- How to set rollback function on inserting records to table?

I have a program where I extract some records from a PDF file, then I proceed to insert those records into a table in MySQL.
One of my main concern is if there is an error under any circumstances during the inserting to table. Let's say if I am inserting 1000 records from a file into the table and halfway, something bad happens. So does it auto rollback or do I need to include a "Begin Transaction and Commit Transaction" statement?
If so, how do I initiate a rollback inside Java? I am thinking of writing a rollback function just to achieve this.
My code:
public void index(String path) throws Exception {
PDDocument document = PDDocument.load(new File(path));
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
String sql="insert IGNORE into test.indextable values (?,?)";
// con.connect().setAutoCommit(false);
preparedStatement = con.connect().prepareStatement(sql);
int i=0;
for (String word : words) {
// check if one or more special characters at end of string then remove OR
// check special characters in beginning of the string then remove
// insert every word directly to table db
word=word.replaceAll("([\\W]+$)|(^[\\W]+)", "");
preparedStatement.setString(1, path);
preparedStatement.setString(2, word);
preparedStatement.addBatch();
i++;
if (i % 1000 == 0) {
preparedStatement.executeBatch();
System.out.print("Add Thousand");
}
}
if (i > 0) {
preparedStatement.executeBatch();
System.out.print("Add Remaining");
}
}
}
// con.connect().commit();
preparedStatement.close();
System.out.println("Successfully commited changes to the database!");
}
This function above will be called by another function to be executed and the try and catch exception is in the caller function.
My rollback function:
// function to undo entries in inverted file on error indexing
public void rollbackEntries() throws Exception {
con.connect().rollback();
System.out.println("Successfully rolled back changes from the database!");
}
I appreciate any suggestions.
I don't know what library you are using, so I am just going to guess on exception names and types. If you look in the api you can check to see what exceptions are thrown by what functions.
private final static String INSERT_STATMENT = "insert IGNORE into test.indextable values (?,?)";
public void index(String path) { // Don't throw the exception, handle it.
PDDocument document = null;
try {
document = PDDocument.load(new File(path));
} catch (FileNotFoundException e) {
System.err.println("Unable to find document \"" + path "\"!");
return;
}
if (document == null || document.isEncrypted()) {
System.err.println("Unable to read data from document \"" + path "\"!");
return;
}
String[] lines = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
lines = stripper.getText(document).split("\\r?\\n");
} catch (IOException e) {
System.err.println("Could not read data from document \"" + path "\"! File may be corrupted!");
return;
}
// You can add in extra checks just to test for other specific edge cases
if (lines == null || lines.length < 2) {
System.err.println("Only found 1 line in document \"" + path "\"! File may be corrupted!");
return;
}
for (String line : lines) {
PreparedStatement statement = con.connect().prepareStatement(INSERT_STATMENT );
String[] words = line.split(" ");
for (int index = 0, executeWait = 0; index < words.length; index++, executeWait++) {
preparedStatement.setString(1, path);
preparedStatement.setString(2, words[index].replaceAll("([\\W]+$)|(^[\\W]+)", ""));
preparedStatement.addBatch();
// Repeat this part again like before
if (executeWait % 1000 == 0) {
for (int timeout = 0; true; timeout++) {
try {
preparedStatement.executeBatch();
System.out.print("Pushed " + (((executeWait - 1) % 1000) + 1) + " statements to database.");
break;
} catch (ConnectionLostException e) {
if (timeout >= 5) {
System.err.println("Unable to resolve issues! Exiting...");
return;
}
System.err.println("Lost connection to database! Fix attempt " + (timeout + 1) + ". (Timeout at 5)");
con.reconnect();
} catch (SqlWriteException error) {
System.err.println("Error while writing to database. Rolling back changes and retrying. Fix attempt " + (timeout + 1) + ". (Timeout at 5)");
rollbackEntries();
if (timeout >= 5) {
System.err.println("Unable to resolve issues! Exiting...");
return;
}
}
}
}
}
}
try {
preparedStatement.close();
} catch (IOException e) {
// Do nothing since it means it was already closed.
// Probably throws an exception to prevent people from calling this method twice.
}
System.out.println("Successfully committed all changes to the database!");
}
There are definitely a few more exceptions which you will need to account for which I didn't add.
Edit: Your specific issue can be found at this link

Best way to print data from two columns of mySQL database to jTextfield one after another

I am trying to make a program in which i read an input from the user i.e. in the textfield named gname and store its value to a string called grammar. Now this input is what sorts my output from the database.
Database looks like this
So when user enters G1 in the textfield it should display records in such a way
A->ab,A-ab,B->b
But it only shows 1st element when i use if(myRs.next) and last one if i use while(myRs.next().
current output is
Here is the code for this:
its all in try catch block
String grammar = gname.getText();
myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/grammar", "root", "");
JOptionPane.showMessageDialog(rootPane, "Connected to database");
mystmt = myCon.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT starting_symbol, value from starting_symbol where grammar= '" + grammar + "'";
String query2 = "SELECT non_terminals, terminals from input_values where grammar= '" + grammar + "'";
mystmt.addBatch(query);
mystmt.addBatch(query2);
myCon.setAutoCommit(false);
mystmt.addBatch(query);
mystmt.addBatch(query2);
myRs = mystmt.executeQuery(query);
while (myRs.next()) {
String s = myRs.getString("starting_symbol");
String val = myRs.getString("value");
output.setText(s + "-> " + val);
}
myRs = mystmt.executeQuery(query2);
ArrayList<String> list_one = new ArrayList<String>();
ArrayList<String> list_two = new ArrayList<String>();
while (myRs.next()) {
list_one.add(myRs.getString("non_terminals"));
list_two.add(myRs.getString("terminals"));
for (int i = 0; i < list_one.size(); i++) {
output_2.setText(list_one.get(i) + "->" + list_two.get(i));
}
}
Please help me in getting the correct outut
Use StringBuilder Luke
StringBuilder b = new StringBuilder();
while (myRs.next()) {
String s = myRs.getString("starting_symbol");
String val = myRs.getString("value");
if (b.length() > 0) {
b.append(',');
}
b.append(s + "-> " + val);
}
output.setText(b.toString());
And do the same for output_2 field
Your code snippet can be just like the following:
StringBuilder sb = new StringBuilder();
while (myRs.next()) {
if (sb.length() > 0) sb.append(",");
sb.append(myRs.getString("non_terminals"))
.append("->")
.append(myRs.getString("terminals"));
}
Instead of calling output_2.setText multiple times that only set the text to be the last fetch value of non_terminals -> terminals in this case B->b.

How to determine which part of a WHERE clause caused a row not to be found?

I know that if the SQL String statement is satisfied, next() will continue to check each row. Otherwise, false if it can't satisfy the SQL Select Statement. ( I hope i didn't miss anything)
If my statement is
ResultSet myRs1 = null;
String SQL1 = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ? AND USERTYPE = ?";
If username value on textfield matches username in the row BUT password doesn't match in the same column, what method should I use?
Also, if rowCounter returns false and counts false as 0, how do I compare the password? I'm thinking of getters
I want to do something like
if((usernameInTextField found in current row) BUT (passwordInTextField doesn't match password in current row))
Print message "Password Incorrect"
Please be patient with me as I'm new to JDBC.
Here's my code:
ResultSet myRs1 = null;
String SQL1 = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ? AND USERTYPE = ?";
try {
myConnection = DriverManager.getConnection(dbURL,username,password);
JOptionPane.showMessageDialog(null, "Successfully Connected To Database");
PreparedStatement myPrepStmt = myConnection.prepareStatement(SQL1);
myPrepStmt.setString(1,userNameEntered); //assigns a string value to the first ?
myPrepStmt.setString(2,passwordEntered); //assigns a string value to the second ?
myPrepStmt.setString(3,userTypeEntered);
myRs1 = myPrepStmt.executeQuery(); // executes the select query and stores it to myRs
int rowCounter = 0;
while(myRs1.next()) //as long as query is satisfied, next() will return true
{
rowCounter++;
}
if(rowCounter == 0)
{
JOptionPane.showMessageDialog(this, "Username, Password or Usertype maybe incorrect");
}
else if(rowCounter > 1) //if there are duplications
{
JOptionPane.showMessageDialog(null, "User details found but has more 1 one entry" +
"\nFound: " + rowCounter + " rows" );
}
else if(rowCounter == 1){
JOptionPane.showMessageDialog(null, "Found " + rowCounter + " record");
}
} //end of try
catch (SQLException e) {
}
My code works using rowCounter but can't specify which is incorrect(username/password/usertype).
Thanks.

Categories