I have dropdown menu that has list of column names from certain table in the database and I have textbox where user can type what one is looking for. User first selects the column from dropdown menu and types on textbox what s/he is looking for. I am kind of lost how can i match those dropdown column name and textbox string to look under specific column in oracle database.. Any sample code or suggestion would be helpful.. Thank you
String colname = request.getParameter("colname");//get the column name from teh dropdown
String value = request.getParameter("value");//get the value that the user entered
PreparedStatement searchQuery = null;
String searchString = String.format("Select * from yourtable where %s = ?", colname);
//create a connection , say con
searchQuery = con.prepareStatement(searchString);
searchQuery.setString(1, value);
ReultSet rs = searchQuery.executeQuery();
This solution is only basic a idea so you have to modify to suit. If the columns that you are searching are of different types then you have to cater for that.
Related
I am trying to log in with a name and an ID in a JTextField after a ButtonClick. After the first log in all the users are stored in the database.
What I am trying to do is: If the user is already stored in the database I want to show the message ("Welcome back") but what I got at the moment is "Duplicate entry '11' for key 'PRIMARY'. I want to "ignrore that" because if a user is stored in the database it should have the possibility to log in again and continue for the next page. And if a user is new, I will show a JOptionPane ("Welcome new user")
What I tried so far:
JOptionPane.showMessageDialog(null,"already exist");
}
String query1 = "SELECT * FROM user";
PreparedStatement pst1 = con.prepareStatement(query1);
ResultSet r1= pst1.executeQuery();
if(....)JOptionPane.showMessageDialog(null,"...");}
else if(r1.next()) {
if (r1.getString("ID").equals(txtFieldID.getText())) {
Thx all in advance.
You should first execute a query to validate that the ID already exists in database so in that case you'll only get one result: SELECT COUNT(ID) FROM USERS WHERE ID = ?. If the result of that query returns a value greater than 0 then the user exists and you show the message.
You should't query the whole table and then iterate each row to do that kind of validation.
Targeting 4.2 using Eclipse
I have a basic input screen that takes address book information from the user and puts it into a db table called myAddressBook my code for creating the table is as follows (db is the SQLiteDatabase declaration:
//MODE_PRIVATE = only this application
db = openOrCreateDatabase("AdddressBookDB", MODE_PRIVATE, null);
//Create the Table
db.execSQL("CREATE TABLE IF NOT EXISTS myAddressBook(Name VARCHAR, Address1 NVARCHAR, Address2 NVARCHAR, City VARCHAR, State VARCHAR, Zip INT(5), Phone INT(10));");
Now I have a Search button on the form and an EditText that I want to use to search my table and display the results in a list, or possibly in the original EditTexts used for the input. In the OnClick for the Search button, I have:
String searchvalue = searchEditText.getText().toString();
To get the string of whatever the user wants to search for. Now how do I basically say "find anything in the database that partially or fully matches searchvalue"?? (I am new to Android but VERY new to SQL)
Probably, this is answer for your question
Cursor cursor = getReadableDatabase().
rawQuery("select * from todo where _id = ?", new String[] { id });
This is good article. Please read that for getting answer for your question.
I am trying to select data from my db, but instead of getting a certain field I get "".
My table name is locations and it's look like this:
id - int,
location - varchar and time - timestamp.
I would like to select the last location based on the time, here is my code:
this.select = this.conn.createStatement();
ResultSet result = select.executeQuery("SELECT location FROM locations ORDER BY time DESC Limit 1");
result.next();
System.out.println(result.getString(1));
I remind you the output is "";
To identify the column in your resultSet you can use:
result.getString("location");
did u try ?
System.out.println(result.getString(0));
I have a SQL select query already written in java. Now i want to select few more columns. In that case i need to write column names before the index of from keyword.
Like this
Initial query-
StringBuffer query;
query = " Select name,age,dob from employee"
New query should be like this-
query = "select name , age , dob,city from employee"
For this i tried query= query.insert(indexof(dob),"city");
But i think this will overwrite from.
My question comes down to whether insert creates enough space to insert the string or it overwrites the earlier string?
Please help.
instead of getting index value of dob, get index value of from
query= query.insert(original.indexOf("from"),"city ");
Try this:-
StringBuffer query = new StringBuffer("Select name,age,dob from employee");
query.insert(query.indexOf("from"), "city ");
I need help in a simple application based on Vaadin.
I need to have a table bound to SQL query results. SQL query has a parameter which value user chooses from combobox. What I need is to table be refreshed when user change the combobox value.
That is what I have ():
Table table;
JDBCConnectionPool pool;
String query = "select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id";
FreeformQuery q = new FreeformQuery(query, pool);
SQLContainer container = new SQLContainer(q);
table.setContainerDataSource(container);
So, this simple code selects all data from products and clients tables and puts it to the table. But how can I add filtering by clients.client_id selected from combobox, for example? To implement next query:
select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id where client_id = ?;
Thank you for your help.
You could add a Property.ValueChangeListener that would change your query parameters:
comboBox.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
String customQuery = query.replace(":clientId", ((Client)(event.getProperty()).getId(), pks);
table.setContainerDataSource(new SQLContainer(new FreeformQuery(customQuery, pool)));
}
});
And the query would hold the following value: select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id where client_id = :clientId
But be careful with query.replace, if Id is int there is nothing to worrier about, but if it is string, please addSlashes in order to avoid SQLInjection.