I am trying to use concat with a Derby database query. But i get syntax error. How can I use concat in Derby?
The below code is for search purpose
public ArrayList <Tablearray>ListUsers(String ValToSearch){
ArrayList <Tablearray> usersList = new ArrayList <Tablearray>();
Statement st;
ResultSet rs;
try {
Connection con = getConnection();
st = con.createStatement();
String searchQuery = "SELECT * FROM ABYP WHERE CONCAT(Id_Search, Tyl, Apothkh, Parathrhseis,Ti) LIKE '%"+ValToSearch+"%'";
//String searchQuery = "SELECT * FROM ABYP WHERE ||CONCAT|| (Id_Search) LIKE '%||"+ValToSearch+"||%'";
//String searchQuery = "SELECT *FROM ABYP where ID_SEARCH =? ";
rs = st.executeQuery(searchQuery);
Tablearray tablearray;
while (rs.next()){
tablearray = new Tablearray (
rs.getString("Id_Search"),
rs.getString("Tyl"),
rs.getString("Apothkh"),
rs.getString("Parathrhseis"),
rs.getString("Ti")
);
usersList.add(tablearray);
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return usersList;
}
Related
I want to display data from a database table into 2 jframes. I tried to do something like that but it doesn't work:
String statut = introducereOrasPlecare.getText();
String url = "jdbc:mysql://localhost:3306/aeroport";
String user = "root";
String password = "elev1Aa.";
PreparedStatement myStmt = null;
ResultSet rs = null;
try{
Connection myConn = DriverManager.getConnection(url, user, password);
myStmt = myConn.prepareStatement("SELECT COUNT(z.nrzbor), SUM(b.valoare)\n" +
"FROM Zboruri z INNER JOIN CUPOANE c ON(z.nrzbor=c.nrzbor) INNER JOIN Bilete b ON(c.nrbilet=b.nrbilet)\n" +
"WHERE z.de_la = ?;");
myStmt.setString(1, statut);
rs = myStmt.executeQuery();
String d = rs.getString("COUNT(z.nrzbor)");
jTextField1.setText(d);
String e = rs.getString("SUM(b.valoare)");
jTextField2.setText(e);
}
catch(Exception e){
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
Could anyone help me?
hi every one i have write program that connect to database i create jTextField and jTable my question is how i can use the jTextField to serch in database and view in jTable the code i try is below
try{
String host = "jdbc:derby://localhost:1527/PROCAT";
String uName = "zain";
String uPass = "zain";
Connection con = DriverManager.getConnection( host, uName, uPass);
String sql = "Select * from ITEMB where ITEM '"+asdf.getText()+"'";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String pr = rs.getString("PRISE");
String it= rs.getString("ITEMNAME");
String itm = rs.getString("ITEM");
String[] data = {pr, it, itm};
tabMode.addRow(data);
double price = Double.parseDouble(rs.getString("prise"));
totalpay = price + totalpay;
++rowCount;
}
}
catch (Exception e) {
//ignore
}
jTextField3.setText(String.valueOf(totalpay));
}
Don't ignore the Exception. How to you expect to debug your SQL if you don't display error messages???
I would guess the problem is you are missing an "=" from your select statement (ie. "ITEM = asdf.getText()").
However, the better way to use SQL is to use a PreparedStatement so you don't have to worry about all the delimiters. So the SQL might be something like:
String sql = "Select * from ITEMB WHERE ITEM = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, asdf.getText() );
stmt.executeQuery();
stmt.close();
It is much easier to read an maintain.
private void btgetinvActionPerformed(java.awt.event.ActionEvent evt) {
if(tf_rmid.getText().length()==11){
JOptionPane.showMessageDialog(null,"REMITTANCE ID IS VALID!");
try {
DBUtil util = new DBUtil();
Connection con = util.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select bk_det.rm_id from bk_det inner join bk_rep on bk_det.rm_id = bk_rep.rm_id WHERE rm_id = ?");
String rm = tf_rmid.getText().trim();
stmt.setString(1, ""+(rm));
while(rs.next()){
int i = rs.getString("RM ID");
String fn = rs.getString("First Name");
String ln = rs.getString("Last Name");
String title = rs.getString("Title");
String city = rs.getString("City");
txtFirstName.setText(fn);
txtLastName.setText(ln);
txtTitle.setText(title);
txtCity.setText(city);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
JOptionPane.showMessageDialog(null,"PLEASE ENTER VALID REMITTANCE ID!");
}
}
I am trying to search values from my database using the select statement but when i am trying to get the value for ? for the where option i am getting error i think its a syntax mistake can ny one please help???
Execute with this,
select bk_det.rm_id from bk_det inner join bk_rep
on bk_det.rm_id = bk_rep.rm_id WHERE bk_det.rm_id = ?
Probably you are missing table name in where clause
It appears a bit odd that you are setting the parameter to the Statement AFTER you execute the query.
Also
int i = rs.getString("RM ID");
this does not compile (if rs is a java.sql.ResultSet instance)
Use a prepared Statement:
String query = "select bk_det.rm_id from bk_det inner join bk_rep on bk_det.rm_id = bk_rep.rm_id WHERE rm_id = ?";
try {
PreparedStatement preps = con.prepareStatement(query);
preps.setString(1, ""+(rm));
preps.execute();
...
I want to change the position of the cursor to the first row but I don't know why my code is not working.when I add rs2.first():
and also I am getting this error :
This method should only be called on ResultSet objects that are scrollable (type TYPE_SCROLL_INSENSITIVE).
try{
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String url = "jdbc:derby://localhost:1527/test";
Connection conn = DriverManager.getConnection(url);
String query = "select * from APP.RANKING";
Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSet rs2 = stmt2.executeQuery(query);
while (rs.next()){
String BID = rs.getString("BALLOT_ID");
String CN = rs.getString("CANDIDATE_NAME");
String ROID = rs.getString("USER_ID");
Ro1_ID = ROID;
String RA = rs.getString("RANK");
int rowNum = rs.getRow();
int rowNum2;
boolean In_check = false;
while(rs2.next()){
In_ballot.addElement(BID);
}
rs2.First();
In_ballot.addElement(BID);
}
}
catch(Throwable e) {
System.err.println(e.getMessage());
}
this.InB_list.setModel(In_ballot);
By default, calling createStament() in a connection results in every ResultSet having type 'TYPE_FORWARD_ONLY' - This results in the exception you see using first().
Instead, use another versions of createStatement, like this one.
This sample of creating scrollable ResultSets in Derby might help.
Im trying to use PreparedStatement to my SQLite searches. Statement works fine but Im getting problem with PreparedStatement.
this is my Search method:
public void searchSQL(){
try {
ps = conn.prepareStatement("select * from ?");
ps.setString(1, "clients");
rs = ps.executeQuery();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
but Im getting this error:
java.sql.SQLException: near "?": syntax error at
org.sqlite.DB.throwex(DB.java:288) at
org.sqlite.NestedDB.prepare(NestedDB.java:115) at
org.sqlite.DB.prepare(DB.java:114) at
org.sqlite.PrepStmt.(PrepStmt.java:37) at
org.sqlite.Conn.prepareStatement(Conn.java:231) at
org.sqlite.Conn.prepareStatement(Conn.java:224) at
org.sqlite.Conn.prepareStatement(Conn.java:213)
thx
Columns Parameters can be ? not the table name ;
Your method must look like this :
public void searchSQL()
{
try
{
ps = conn.prepareStatement("select * from clients");
rs = ps.executeQuery();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
Here if I do it like this, it's working fine, see this function :
public void displayContentOfTable()
{
java.sql.ResultSet rs = null;
try
{
con = this.getConnection();
java.sql.PreparedStatement pstatement = con.prepareStatement("Select * from LoginInfo");
rs = pstatement.executeQuery();
while (rs.next())
{
String email = rs.getString(1);
String nickName = rs.getString(2);
String password = rs.getString(3);
String loginDate = rs.getString(4);
System.out.println("-----------------------------------");
System.out.println("Email : " + email);
System.out.println("NickName : " + nickName);
System.out.println("Password : " + password);
System.out.println("Login Date : " + loginDate);
System.out.println("-----------------------------------");
}
rs.close(); // Do remember to always close this, once you done
// using it's values.
}
catch(Exception e)
{
e.printStackTrace();
}
}
Make ResultSet a local variable, instead of instance variable (as done on your side). And close it once you are done with it, by writing rs.close() and rs = null.
Passing table names in a prepared statement is not possible.
The method setString is when you want to pass a variable in a where clause, for example:
select * from clients where name = ?
thx for replies guys,,,
now its working fine.
I noticed sql query cant hold ? to columns too.
So, this sql query to PreparedStatement is working:
String sql = "select * from clients where name like ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "a%");
rs = ps.executeQuery();
but, if I try to use column as setString, it doesnt work:
String sql = "select * from clientes where ? like ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "name");
ps.setString(2, "a%"):
rs = ps.executeQuery();
Am I correct? or how can I bypass this?
thx again