I am coding
String Name = txtName.getText();
String Surname = txtSurname.getText();
st.executeUpdate("DELETE from list WHERE Name=#Name and Surname=#Surname");
but it doesn't delete the records. Any problem with the syntax? Please help.
You need to replace #name and #surname with the actual values.
and add ' around your values:
DELETE from list WHERE Name='#Name' and Surname='#Surname'
String Name = txtName.getText().Trim();
String Surname = txtSurname.getText().Trim();
String query = "DELETE from list WHERE Name='" + Name + "' and Surname='" + Surname + "'";
st.executeUpdate(query);
try:
st.executeNonQuery("DELETE from list WHERE Name=?e and Surname=?");
and pass the name and surname as parameters.
Use this
If the table list exists .
This will work .
String Name = txtName.getText();
String Surname = txtSurname.getText();
st.executeUpdate("DELETE from list WHERE Name='"+Name"' and Surname='"+Surname"'");
I fixed it like that:
st.executeUpdate("DELETE from list WHERE Name='"+txtName.getText()+"'" + "and Surname='" + txtSurname.getText()+"'");
Thank you all
Related
I have the following scenario :
Map<String, HashSet<String>> B = new HashMap<String, HashSet<String>>();
if(!B.containsKey(row5.issue)){
B.put(row5.issue,new HashSet<String>());
}
B.get(row5.issue).add(row5.name);
globalMap.put("BMap", B);
Now, I have the following scripts in another Subjob:
"select * from Table where name in ('" + BMap.values() + "');"
The bold portion has problems, anyone can advise? Thanks very much.
Iterate through your BMap and build a String based on it's values to query the database with. I am presuming your HashSet<String> holds the names in your HashMap<String, HashSet<String>>.
String sql_statement = "";
for(HashSet<String> names : BMap.values()){
for(String name : names){
sql_statement += "'" + name + "',";
}
}
sql_statement = sql_statement.substring(0, sql_statement.length-1); // to remove the comma at the end
And the select statement will look like this:
"select * from Table where name in (" + sql_statement +");"
I haven't tested this but it should give you the general idea on how to fix this problem.
Hey am trying to make a javafx program that join two strings but i am getting error when trying to join them with join command. My javafx code is
String name = "John";
String lastname= "doe";
String fullname = name.join(lastname);
I don't know this is correct or it's my foolishness to use this as i am beginner to javafx. I hope you can resolve my problem. Thanks in advance
String name = "John";
String lastname= "doe";
String fullname = String.join(" ", name, lastname);
As in the documentation:
public static String join(CharSequence delimiter, CharSequence... elements)
But you could simply use concatenation:
String fullname = name + " " + lastname;
So i just wrote down this SQL query and i am trying to capture the value of rest_id in query.list(). However, this is giving the value as [1] . I want just 1 without the braces. How do i do it? Please check the code below for reference:
String sql1 = "select rest_id from rest_details where rest_name = '" + nameclicked + "' and rest_location = '" +locclicked + "'" ;
SQLQuery query1 = session.createSQLQuery(sql1);
System.out.println("sql1 " + query1.list());
Use below code to get the element inside list:
System.out.println("sql1 " + query1.list().get(0));
This always returns only the first element from the list.
Replace
System.out.println("sql1 " + query1.list());
By :
for(String id : query1.list() ) System.out.println("sql1 " + id);
I'm trying to make a query with selected fields for final user from view (JSP) to controller, but I don't know how.
For example, I have this parameters from view (JSP)
IDUSER,>,2,OR,USERNAME,=,'KURT'
So, I'll want to have something like this,
SELECT IDUSER, USERNAME FROM TABLE_NAME WHERE IDUSER > 2 OR USERNAME = 'KURT'
but I have next result
SELECT null FROM TABLE_NAME WHERE IDUSER > 2 OR USERNAME = 'KURT'
I'm parsing string with StringTokenizer class, where query is: String query = request.getParameter("data"); and data is IDUSER,>,2,OR,USERNAME,=,'KURT'.
StringTokenizer field = new StringTokenizer(query, ",");
nFields = field.countTokens();
System.out.println("nFields: " + nFields);
String[] fields = new String[nFields];
for(int i = 0; i < fields.length; i++) {
while(field.hasMoreTokens()) {
fields[i] = field.nextToken();
}
System.out.println("fields[i]: " + fields[i]);
myQuery = "SELECT " + fields[i] + " FROM "+tableName+ " WHERE ";
System.out.println("myQuery 1: " + myQuery);
}
StringTokenizer token= new StringTokenizer(query, "|,");
while(token.hasMoreTokens()) {
myQuery = myQuery + token.nextToken() + " ";
}
System.out.println("QUERY RESOLVED: " + myQuery);
PLEASE HELP ME
Here is the solution after minor tweak in your query (redefined the separators)
public static void main(String[] args) {
// Redefine the separators as single , separators is difficult to process
//You would need to define possible operators like this (#OR# , #AND# ) ,surrounded by special characters to identify.
String query ="IDUSER_>_2#OR#USERNAME_=_'KURT'";
String tableName="TESTTABLE";
String operator=null;
//you can choose operator conditionally
if(query.contains("#OR#")) operator="#OR#";
// if(query.contains("#AND#")) operator="#AND#";
//Used split instead of Tokenizer.
String cols[]= query.split(operator);
String myQuery = "SELECT ";
String select="";
for(String col:cols){
if(!select.isEmpty()){
select+=" , ";
}
// Only the first element is retrieved (for select)
select+=col.split("_")[0];
}
myQuery+=select+" FROM "+tableName+ " WHERE ";
// Removes all special charecters (like, # and _ with white space)
String subQuery = query.replaceAll("#", " ");
subQuery=subQuery.replaceAll("_", "");
myQuery+=subQuery;
System.out.println("QUERY RESOLVED: " + myQuery);
}
Note : ',' is replaced with '_' and operators are surrounded by '#'
Cheers!!
I think the problem is this line in your while loop:
myQuery = "SELECT " + fields[i] + " FROM "+tableName+ " WHERE ";
This will keep changing the value of myQuery as the while loop executes.
Maybe you need to replace this with:
myQuery = "SELECT " + fields[0] + " FROM "+tableName+ " WHERE ";
break;
I am assuming your selection criteria is the first field in the parameter from your view.
Do not see where does IDPERFIL come from. Also, I don't like this:
while(field.hasMoreTokens()) {
fields[i] = field.nextToken();
}
That will iterate tokenizer to the end, and stop at last element. I am sure you don't want this. Fix that, tell where IDPERFIL come from, and then, maybe, you'll understand answer by yourself. Otherwise, I'll try to help further.
Hi I am searching for a name combination in database. I am passing the combination as follows
"firstName='" + firstName + "'", "middleName='" + middleName + "'", "lastName='" + lastName + "'"
This works fine. But the problem comes where there are some " ' " in names how can i rectify it? eg: Johns' or Jerry's etc causes exception
use preparedStatement it is easy for you
ps.executeUpdate("INSERT INTO tb_name values(?,?) WHERE id=?");
ps.setString(1,firstName);
ps.setString(2,middleName);
ps.setInt(3,id);
ps.execute();
At least for MySQL, you have to put another ' before:
INSERT INTO table (column) VALUES ('this isn''t it');
If you're using Hibernate, you should use like this:
Query query = session.createQuery("from Something s where s.firstName = :firstName and s.middleName = :middleName and s.lastName = :lastName");
query.setString("firstName", firstName);
query.setString("middleName", middleName);
query.setString("lastName", lastName);
List<?> list = query.list();
Hope this can help you!
You can see more at here and here
String firstName="X";
String middleName="Y";
String lastName="Z";
"firstName='" + firstName + "',middleName='" + middleName + "',lastName='" + lastName + "'";
You can use this to get output as
firstName='X',middleName='Y',lastName='Z'
You can ignore sinqle quotation characters (') in SQL by escaping them with a backslash \'.
Try this:
firstName = firstName.replace("'" , "''");
Use PreparedStatement instead , you would be better with that.
I guess the following also works (at least for MySQL):
SELECT login FROM usertable WHERE lastname="O'Neil";