How can I search a customer by full name? - java

I'm creating a method wherein a user can search a customer either by first name, last name, or full name. the search bar only shows the result if it's by first or last name but when I try searching a customer by full name the record won't show.
Here's my code:
public int getMatchingCustomerRecords(String keyword) {
int rows = 0;
try {
if (db.startTransaction()) {
String sql = "Select COUNT(firstName OR lastName) AS TOTAL FROM Customer";
String sqlSearch = "";
if (keyword != null) {
sqlSearch = sql + " WHERE firstName LIKE '%" + keyword + "%' AND lastName LIKE '%" + keyword + "%'";
ps = db.getQueryStatement(sqlSearch);
} else {
ps = db.getQueryStatement(sql);
}
rs = ps.executeQuery();
if (rs.next()) {
rows = rs.getInt("TOTAL");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}

Your queries should be:
Select * FROM `Customer`
WHERE `firstName` LIKE '%" + keyword + "%' OR `lastName` LIKE '%" + keyword + "%'"

public int getMatchingCustomerRecords(String keyword) {
int rows = 0;
try {
if (db.startTransaction()) {
String sql = "Select COUNT(firstName OR lastName) AS TOTAL FROM Customer";
String sqlSearch = "";
if (keyword != null) {
String[] keywordArray=keyword.split(" ");
if(keywordArray.length>1) {
// matching full Name
sqlSearch = sql+" WHERE firstName LIKE '%" + keywordArray[0].trim() + "%' AND lastName LIKE '%" + keywordArray[1].trim() + "%'";
}else {
// matching firstName or lastName
sqlSearch = sql+" WHERE firstName LIKE '%" + keywordArray[0].trim() + "%' OR lastName LIKE '%" + keywordArray[0].trim() + "%'";
}
ps = db.getQueryStatement(sqlSearch);
} else {
ps = db.getQueryStatement(sql);
}
rs = ps.executeQuery();
if (rs.next()) {
rows = rs.getInt("TOTAL");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}

public int getMatchingCustomerRecords(String keyword) {
int rows = 0;
try {
if (db.startTransaction()) {
String sql = "Select COUNT(firstName OR lastName) AS TOTAL FROM Customer";
String sqlSearch = "";
if (keyword != null) {
sqlSearch = sql + " WHERE LOCATE(firstName ,'" + keyword + "')>=0 AND LOCATE(lastName ,'" + keyword + "')>=0";
ps = db.getQueryStatement(sqlSearch);
} else {
ps = db.getQueryStatement(sql);
}
rs = ps.executeQuery();
if (rs.next()) {
rows = rs.getInt("TOTAL");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}

Related

How do I store values from a database into a collection?

public String getDataOfpersons() {
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while(rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
System.out.println("Name: " + name + " " + "age: " + age);
}
}catch(Exception ex) {
System.out.println(ex);
}
return null;
}
So I have this part of code and I am reading it from mysql database and I am wondering how could I store object to collection (Name and age) ?
First, create a Person class with "name" and "age" fields. Also add the corresponding getters and setters.
Then modify modify your code like this:
// change the return type
public List<Person> getDataOfpersons() {
// create a list to store values
List<Person> personList = new ArrayList<>();
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while (rs.next()) {
Person person = new Person();
String name = rs.getString("name");
String age = rs.getString("age");
person.setName(name);
person.setAge(age);
personList.add(person);
System.out.println("Name: " + name + " " + "age: " + age);
}
} catch(Exception ex) {
System.out.println(ex);
}
return personList;
}
Don't know if this is what u looking for without changing the existing signature of the method
public String getDataOfpersons() {
try {
Map<String,String> map = new HashMap<>();
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while(rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
//System.out.println("Name: " + name + " " + "age: " + age);
map.put("Name",name);
map.put("age",age);
System.out.println(map);
}
}catch(Exception ex) {
System.out.println(ex);
}
return null;
}

out of memory when trying to select data from oracle database and insert it into sqlite

we get out of memory (the process go up and consumes %100 of ram) trying to select data and insert it into SQLite DB, the program written in java, and the data are so big, we even made pagination for it, it selects 100000 rows and inserts it into SQLite database, to figure out the problem, we commented out all line from the code that insert the data, and we saw that consumption of ram stop's at %6
package dbex;
import java.sql.*;
import java.util.ArrayList;
public class DBEX {
String url = "jdbc:oracle:thin:#192.168.120.46:1521:db";
String user = "dbuser";
String password = "dbpass";
ArrayList<Table> Tales = new ArrayList<>();
public static void main(String[] args) {
DBEX db = new DBEX();
db.CreateDB();
}
public int CountRecords(String table) {
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection getCountCon = DriverManager.getConnection(url, user, password);
Statement CountSt = getCountCon.createStatement();
ResultSet countRs = CountSt.executeQuery("SELECT COUNT(*) as num FROM " + table);
while (countRs.next()) {
result = countRs.getInt("num");
}
} catch (Exception ex) {
}
return result;
}
public void dumpData() {
try {
int incrementRecord = 100000;
Class.forName("org.sqlite.JDBC");
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Table table = null;
ArrayList<String> columns = new ArrayList<>();
Connection getDataCon = DriverManager.getConnection(url, user, password);
getDataCon.setAutoCommit(false);
Statement dataSt = getDataCon.createStatement();
dataSt.setFetchSize(incrementRecord);
ResultSet dataRs = null;
Connection LiteInsertCon = DriverManager.getConnection("jdbc:sqlite:" + "DBEX" + ".db");
LiteInsertCon.setAutoCommit(false);
PreparedStatement insertStatement = null;
int numRecord = 0;
for (int i = 0; i < Tales.size(); i++) {
table = Tales.get(i);
String tableName = table.name;
System.out.println("table::" + tableName);
System.out.println("Dumping data from " + tableName);
String InsertSQL = "INSERT INTO " + tableName + " ( ";
String vals = "VALUES (";
String selectSQL = "SELECT * FROM ( SELECT b.*, ROWNUM RN FROM ( SELECT ";
System.out.println("geting column name...");
columns = table.Columns;
for (int j = 0; j < columns.size(); j++) {
String columnName = columns.get(j);
if (j == 0) {
vals += " ? ";
InsertSQL += columnName + " ";
selectSQL += columnName + " ";
}else {
InsertSQL += " , " + columnName + " ";
vals += ", ? ";
selectSQL += " , " + columnName + " ";
}
}
System.out.println("Number of column of table " + tableName + " is " + columns.size());
vals += ")";
InsertSQL += ")" + vals;
selectSQL += "FROM " + tableName + " ORDER BY "+table.primary_key+" ASC ) b WHERE ROWNUM <= :TO ) WHERE RN > :FROM ";
System.out.println(selectSQL);
System.out.println(InsertSQL);
int parIndex = 0;
String colName = null;
String data = null;
numRecord = CountRecords(tableName);
int from = 0;
int to = incrementRecord;
if (to > numRecord) {
System.out.println("Table data is less than "+incrementRecord+" geting all data "+numRecord+" " );
dataRs = dataSt.executeQuery(selectSQL.replace(":TO", numRecord + "").replace(":FROM", "0"));
while (dataRs.next()) {
insertStatement = LiteInsertCon.prepareStatement(InsertSQL);
insertStatement.setFetchSize(incrementRecord);
for (; parIndex < columns.size(); parIndex++) {
colName = columns.get(parIndex);
data = dataRs.getString(colName);
if (data != null) {
insertStatement.setString(parIndex + 1, data);
} else {
insertStatement.setString(parIndex + 1, " ");
}
parIndex++;
}
parIndex = 0;
insertStatement.executeUpdate();
insertStatement = null;
}
} else {
while (numRecord >= to) {
System.out.println("page "+(to/incrementRecord)+" of "+(numRecord/incrementRecord) );
dataRs = dataSt.executeQuery(selectSQL.replace(":TO", to + "").replace(":FROM", from+""));
while (dataRs.next()) {
insertStatement = LiteInsertCon.prepareStatement(InsertSQL);
insertStatement.setFetchSize(incrementRecord);
for (; parIndex < columns.size(); parIndex++) {
colName = columns.get(parIndex);
data = dataRs.getString(colName);
if (data != null) {
insertStatement.setString(parIndex + 1, data);
} else {
insertStatement.setString(parIndex + 1, " ");
}
parIndex++;
}
parIndex = 0;
insertStatement.executeUpdate();
insertStatement = null;
}
dataRs=null;
from = to;
to += incrementRecord;
System.gc();
}
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
class Table {
public String name;
public String primary_key;
ArrayList< String> Columns = new ArrayList<>();
public Table(String name, String primary_key, ArrayList< String> Columns) {
this.name = name;
this.primary_key = primary_key;
this.Columns = Columns;
}
}
public void CreateDB() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Oracle JDBC driver found");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection getTableCon = DriverManager.getConnection(url, user, password);
Connection getColumnCon = DriverManager.getConnection(url, user, password);
Connection getKeyCon = DriverManager.getConnection(url, user, password);
System.out.println("connected to database");
Statement getTableSt = getTableCon.createStatement();
PreparedStatement getColumnSt = getColumnCon.prepareStatement("SELECT DISTINCT column_name FROM all_tab_cols WHERE table_name = ? ");
PreparedStatement getKeySt = getKeyCon.prepareStatement("SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cols.table_name = ? AND cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position");
ResultSet getTableRs = getTableSt.executeQuery("SELECT * FROM all_tables WHERE OWNER not in('SYS','OUTLN','SYSTEM')");
ResultSet getColumnRs;
ResultSet getKeyRs;
Class.forName("org.sqlite.JDBC");
System.out.println("Sqlite JDBC driver found");
Connection LiteCreateTableCon = null;
LiteCreateTableCon = DriverManager.getConnection("jdbc:sqlite:" + "DBEX" + ".db");
Statement statement = LiteCreateTableCon.createStatement();
String keyName = "";
while (getTableRs.next()) {
String tableName = getTableRs.getString("table_name");
System.out.println("table: " + tableName);
getColumnSt.setString(1, tableName);
System.out.println("SELECT * FROM all_tab_cols WHERE table_name = " + tableName + "");
getColumnRs = getColumnSt.executeQuery();
String createTableSQL = "create table if not exists " + tableName + " ( ";
int index = 0;
ArrayList<String> Columns = new ArrayList<>();
while (getColumnRs.next()){
String columnName = getColumnRs.getString("column_name");
Columns.add(columnName);
System.out.println("column: " + columnName);
if (index == 0) {
createTableSQL += columnName + " string";
} else {
createTableSQL += " , " + columnName + " string";
}
index++;
}
createTableSQL += ")";
System.out.println(createTableSQL);
statement.executeUpdate(createTableSQL);
getKeySt.setString(1, tableName);
getKeyRs = getKeySt.executeQuery();
keyName = "1";
while (getKeyRs.next()) {
keyName = getKeyRs.getString("column_name");
}
Tales.add(new Table(tableName, keyName, Columns));
}
getTableCon.close();
getColumnCon.close();
getKeyCon.close();
System.out.println("All table created...");
}catch (Exception ex) {
System.out.println(ex.toString());
}
dumpData();
}
}
You need to issue/excute commit on SQLite after inserting every batch of records.
And try to come-up with suitable batch size (may be 100).

Getting Parameter value in java with symbol Apostrophe(') query

I have parameter named paramValue then I am trying to store it this is working if my paramvalue is equals to any value without apostrophe like paramvalue = 'klang#'
but if it has apostrophe it wont work like this paramvalue = 'kl'ang#'
I put some part of my code
query += "'" + paramValue + "'";
I tried this but I am getting error like '; expected' Is there any other option to do it like this below I will really appreciate any advice thank you
query += """ + paramValue + """;
below is my code
public boolean insertInto(String tableName, String paramsName[], String paramsValues[], String id) {
String query = "INSERT INTO tbl1" + tableName + " (id, dateCreated, dateModified";
for (String paramName : paramsName) {
query += ", c_" + paramName;
}
query += ") VALUES( ";
if (id == null || id.isEmpty()) {
query += "UUID(), NOW() , NOW()";
} else {
query += "'" + id + "', NOW(), NOW()";
}
for (String paramValue : paramsValues) {
query += "'" + paramValue + "'";
}
query += ")";
Connection con = null;
//myconn
try {
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
return stmt.execute();
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}

Java printing database records on separate lines

I have a JDBC program that takes records from a MySQL database and prints out the results. The user can select which results they want from the database by selecting different checkboxes to only display certain results.
Here is the method which gets the records and prints them out:
private void execute() throws SQLException {
String query = "SELECT * FROM customers";
ResultSet rs = stmt.executeQuery(query);
String result = "";
while (rs.next()) {
if (cb1.isSelected()) {
int custid = rs.getInt("custid");
result += custid + " ";
}
if (cb2.isSelected()) {
String name = rs.getString("name");
result += name + " ";
}
if (cb3.isSelected()) {
String address = rs.getString("address");
result += address + " ";
}
if (cb4.isSelected()) {
String city = rs.getString("city");
result += city + " ";
}
if (cb5.isSelected()) {
String state = rs.getString("state");
result += state + " ";
}
if (cb6.isSelected()) {
int zip = rs.getInt("zip");
result += zip + " ";
}
// print the results
}
System.out.println(result);
results.setText(result);
stmt.close();
}
Currently, if I were to select say the first three checkboxes, I would get the output:
1 Smith, Tim 12 Elm St 2 Jones, Tom 435 Oak Dr 3 Avery, Bill 623 Ash Ave 4 Kerr, Debra 1573 Yew Crt
However, the output I am after is:
1, Smith, Tim, 12 Elm St
2, Jones, Tom, 435 Oak Dr
3, Avery, Bill, 623 Ash Ave
4, Kerr, Debra, 1573 Yew Crt
Is there any way I can add a new line after each record in the database, as well as maybe the commas in between items in each record? I am new to JDBC and MySQL connectivity, so any help or tips is appreciated.
You can print every single result just before the end of while loop, then it'll print every record in new line.
private void execute() throws SQLException {
String query = "SELECT * FROM customers";
ResultSet rs = stmt.executeQuery(query);
String result = "";
String singleResult = "";
while (rs.next()) {
if (cb1.isSelected()) {
int custid = rs.getInt("custid");
singleResult += custid + " ";
}
if (cb2.isSelected()) {
String name = rs.getString("name");
singleResult += name + " ";
}
if (cb3.isSelected()) {
String address = rs.getString("address");
singleResult += address + " ";
}
if (cb4.isSelected()) {
String city = rs.getString("city");
singleResult += city + " ";
}
if (cb5.isSelected()) {
String state = rs.getString("state");
singleResult += state + " ";
}
if (cb6.isSelected()) {
int zip = rs.getInt("zip");
singleResult += zip + " ";
}
System.out.println(singleResult);
result +=singleResult;
}
//System.out.println(result);
results.setText(result);
stmt.close();
}
Or you can append line separator, just before closing while loop
System.out.println(singleResult);
result +=singleResult;
result +="\n";
First, I would use a StringJoiner to gather the elements. Then, I would eliminate the many local temporary variables. Finally, I would use println in the loop and another StringJoiner for the final result. Like,
private void execute() throws SQLException {
String query = "SELECT * FROM customers";
ResultSet rs = stmt.executeQuery(query);
StringJoiner result = new StringJoiner(System.lineSeparator());
while (rs.next()) {
StringJoiner lineJoiner = new StringJoiner(", ");
if (cb1.isSelected()) {
lineJoiner.add(String.valueOf(rs.getInt("custid")));
}
if (cb2.isSelected()) {
lineJoiner.add(rs.getString("name"));
}
if (cb3.isSelected()) {
lineJoiner.add(rs.getString("address"));
}
if (cb4.isSelected()) {
lineJoiner.add(rs.getString("city"));
}
if (cb5.isSelected()) {
lineJoiner.add(rs.getString("state"));
}
if (cb6.isSelected()) {
lineJoiner.add(String.valueOf(rs.getInt("zip")));
}
System.out.println(lineJoiner);
result.add(lineJoiner.toString());
}
results.setText(result.toString());
stmt.close();
}
You could also do the same thing with Collection(s) like,
String query = "SELECT * FROM customers";
ResultSet rs = stmt.executeQuery(query);
List<String> msg = new ArrayList<>();
while (rs.next()) {
List<String> al = new ArrayList<>();
if (cb1.isSelected()) {
al.add(String.valueOf(rs.getInt("custid")));
}
if (cb2.isSelected()) {
al.add(rs.getString("name"));
}
if (cb3.isSelected()) {
al.add(rs.getString("address"));
}
if (cb4.isSelected()) {
al.add(rs.getString("city"));
}
if (cb5.isSelected()) {
al.add(rs.getString("state"));
}
if (cb6.isSelected()) {
al.add(String.valueOf(rs.getInt("zip")));
}
String line = al.stream().collect(Collectors.joining(", "));
System.out.println(line);
msg.add(line);
}
results.setText(msg.stream().collect(Collectors.joining(System.lineSeparator())));
stmt.close();
Prefer whichever you find most readable.

send a broadcast message to all items in list in java

I would like to send a broadcast message to all numbers returned from the select statement. It saves elements in the list but then it sends the same message to everyone. What am I doing wrong? Please see my method below.
public static List<Message> listAllMessages(Connection connection) {
List<Message> msg = new ArrayList<Message>();
String messages = ReturnTexts.getMessage(connection, "EMPTYMESSAGE");
String sql = "SELECT b.`productid` as productid, p.`productname` as productname, b.`msisdn` as msisdn , MAX(b.`amount`) as amount, b.`productcode` as productcode, a.`endDate` as enddate FROM "
+ TableNames.SAVEDBIDSTABLE
+ "b LEFT JOIN "
+ TableNames.PRODUCTTABLE1
+ " p ON b.`productcode`= p.`code` "
+ " JOIN "
+ TableNames.AUCTIONTABLE1
+ " a"
+ " ON b.`productcode`= a.`productcode` "
+ "GROUP BY msisdn, productcode ";
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
if (connection == null || connection.isClosed() )
connection = DBConnection.getConnection();
// LOGGER.info(sql);
statement = DBConnection.isConnected(connection).prepareStatement(
sql);
// statement = connection.createStatement();
resultSet = statement.executeQuery();
long productid = 0;
String productname = null;
String msisdn = null;
int amount = 0;
String productcode = null;
Date enddate = null;
while (resultSet.next()) {
productid = resultSet.getLong("productid");
productname = resultSet.getString("productname");
msisdn = resultSet.getString("msisdn");
amount = resultSet.getInt("amount");
productcode = resultSet.getString("productcode");
enddate = resultSet.getTimestamp("enddate");
msg.add(new Message(Long.valueOf(productid), productname,
msisdn, amount, productcode, String.valueOf(enddate)));
}
String messages = ReturnTexts
.getMessage(connection, "BROADCAST")
.replace("XXXX", productname)
// .replace("YYYY", String.valueOf(amount))
.replace("YYYY",
String.valueOf(maxBid(productcode, connection)))
.replace("ZZZZ", String.valueOf(enddate));
//LOGGER.info(messages.toString());
try {
for (Message obj : msg) {
obj.setMessage(messages);
String apiUrl = "url/sendsms.jsp";
getResponse(apiUrl + "?" + "user="
+ URLEncoder.encode("xxx", "UTF-8")
+ "&password="
+ URLEncoder.encode("xxx", "UTF-8")
+ "&mobiles=" + obj.getMsisdn() + "&sms="
+ URLEncoder.encode(obj.getMessage(), "UTF-8"));
//bulkMessagesLog(obj.getMsisdn(), obj.getMessage(),obj.getProductcode(), connection);
bulkMessagesLog(productcode, msisdn, productname, connection);
//LOGGER.info(obj.getMsisdn() + " : " + obj.getProductcode()+ " : " + obj.getMessage());
}
} catch (UnsupportedEncodingException e) {
System.err
.println("UnsupportedEncodingException while trying to send SMS.");
e.getMessage();
}
} catch (SQLException e) {
LOGGER.error(e.getMessage());
} finally {
DBConnection.closeAllDBUsage(resultSet, statement, null);
}
return msg;
}
public static void bulkMessagesLog(String msisdn, String message,String productcode,
Connection connection) {
PreparedStatement statement = null;
String sql = "INSERT INTO " + TableNames.BULK_MESSAGESLOGTABLE
+ "(`msisdn`,`message`,`productcode`,`dateCreated`) VALUES(?,?,?,now()) ";
try {
if ( connection == null || connection.isClosed() )
connection = DBConnection.getConnection();
statement = DBConnection.isConnected(connection).prepareStatement(
sql);
statement.setString(1, msisdn);
statement.setString(2, message);
statement.setString(3, productcode);
//statement.addBatch();
statement.executeUpdate();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
} finally {
DBConnection.closeAllDBUsage(null, statement, connection);
}
}
You do iterate over the result set and build a list of messages in msg. Though you only create the text once, outside of the loop, so it's always the same with the (last) productname etc.
Should probably also be created in the loop.

Categories