I have a frame with 50 label, i can select them, for example i select A1,A2,A3,A4 the program save these in string (String=A1;A2;A3;A4) and i want to call in this query
try{
String sql= "UPDATE Teremszekek SET Állapot='Foglalt' where Terem='1' and Szék='A1' and DKód='1' ";
PreparedStatement pst=conn.prepareStatement(sql);
pst.execute();
}catch(Exception e){
System.out.println(e);
}
}
Állapot=condition(available,unavailable) Terem=room(index of room) Szék=chair DKód=Date code
How can i write that if i choose, A1,A2,A3,A4 etc label then it paste to query
//i select them at the same time
If I understand your question in the right way, this should be your solution:
String sql= "UPDATE Teremszekek SET Állapot='Foglalt' where Terem='1' and Szék=? and DKód='1' ";
pst.setString(1, yourValue);
Related
I need to assign a string taken by a query from the database to a Jlabel. I tried many methods but failed. How can i do it?
try{
String sql="SELECT MAX(allocationID) FROM allocation where unit='"+ dept + " ' ";
pst=conn.prepareStatement(sql);
String x= (pst.execute());
}
catch(Exception e){
}
Need to study the steps to connect to the database in java First db steps
Get the resultset from the statment by calling ResultSet rs = pst.execute();
Iterate through the list of rows by using the resultset object.
After that assign the value to the JLabel.
You just made several errors in your tiny program, take a look at the code below as an example:
// your way of using prepared statement is wrong.
// use like this
String sql="SELECT MAX(allocationID) FROM allocation where unit=?;";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
// assign values to the variables in the query string
ps.setString(1, dept);
// execute the query
ResultSet rst = ps.executeQuery();
// parse the result set to get the value
// You'd better do some check here to ensure you get the right result
rst.next();
String x = rst.getInt(1) + "";
ps.close();
conn.close();
}
Have a look at the article if you are interested:https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
i have the following jframe :
and i wanna make the buttons work im still new to programming can someone help me please? i want the add row btn to add a new row to database, the update btn let me save changes and delete delete the selected row, also the jTextBoxes are connected to the database
i tried doing this to update :
Connection conn=null;
PreparedStatement pst = null;
try{
String value1=txt_cid.getText();
String value2=txt_carid.getText();
String value3=txt_aid.getText();
String value4=txt_rd.getText();
String value5=txt_bd.getText();
String value6=txt_bn.getText();
String sql="update booking set customer_id'"+value1+"',car_id'"+value2+"',agency_id'"+value3+"',return_date'"+value4+"',booking_date'"+value5+"',booking_number'"+value6+"',";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "table updated");
}catch(Exception e) {
JOptionPane.showMessageDialog(null,e);
}
but it didnt work out for me i get exception error
You haven't stated what the error is but UPDATE takes an equals operator for every parameter. Also use PreparedStatement placeholders to avoid SQL Injection attacks:
String sql = "update booking set customer_id=?, car_id=?,agency_id=?,return_date=?,booking_date=?,booking_number=?";
pst = conn.prepareStatement(sql);
pst.setInt(1, value1);
pst.setInt(2, value2);
... // set the other parameters
Read: UPDATE Syntax
I am new in java, in a java project, i want to subtract a textbox value named Quantity(q_field) from 'Available' field of database table Item_detail whenever 'sell' button is clicked and automatically update the table. I wrote some piece of code but its not working. My code is:
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
if(!p_field.getText().isEmpty() && !b_field.getText().isEmpty() && !m_field.getText().isEmpty() && !sell_field.getText().isEmpty() && !c_field.getText().isEmpty()){
int a=Integer.parseInt(q_field.getText().trim());
String sql1="update Item_detail set Available=Available-'a' where P_name=? and Manuf_name =? and Model_no=?";
String sql2="insert into Sell (`S_id`,`P_name`,`Manuf_name`,`Model_no`,`Date`,`Quantity`,`S.p`,`Cost_price`) values(?,?,?,?,?,?,?,?)";
try{
pst=(PreparedStatement) con.prepareStatement(sql1);
pst.setString(1, p_field.getText());
pst.setString(2, b_field.getText());
pst.setString(3, m_field.getText());
pst.setString(4, q_field.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Product sold successfully");
update_table();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
What will be the correct sql code of 'sql1', i can not understand. Please help
sql1 shall be:
String sql1="update Item_detail
set Available=Available-?
where
P_name=?
and Manuf_name =?
and Model_no=?";
And set values to pst query to include value of variable a as follows:
pst=(PreparedStatement) con.prepareStatement(sql1);
pst.setInt(1, a);
pst.setString(2, ...
...
pst.executeUpdate();
But make sure that you have values set only for that number of palce holders in the query. Otherwise there would be a place holder count mismatch and an SQLException would be thrown.
update Item_detail set Available=Available - ? where ...
The value of a is a parameter of your query, just like the other ones. BTW, you're binding 4 different parameters to the statement, and your query only has 3 parameters (? placeholders)
Change your Update Query to this
String sql1="update Item_detail set Available=Available-? where P_name=? and Manuf_name =? and Model_no=?";
ps.setInt(1,a);
I am trying to Update my data from my netbeans to sqlite. there is no problem with the query but when I run the program a message box will appear "java.sql.SQLException:query does not return results". What seems to be the problem?
try{
String value1=txtId.getText();
String value2=txtFirst.getText();
String value3=txtLast.getText();
String value4=txtUser.getText();
String value5=txtPass.getText();
String sql="Update account set id='"+value1+"', fname='"+value2+"', lname='"+value3+"',username='"+value4+"', password='"+value5+"' where id='"+value1+"' ";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
JOptionPane.showMessageDialog(null,"Data Updated");
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
int updateCount = pst.executeUpdate();
Instead of executeQuery.
Update query:
st.executeUpdate("update reservation set busname='"+jTextField10.getText()+"',busno='"+jTextField9.getText()+"',cusname='"+jTextField8.getText()+"',noofpass='"+jTextField7.getText()+"',amount='"+jTextField6.getText()+"' where cusname='"+jTextField8.getText()+"' ");
You can also try:
String strQuery = ("update visitor set name='"+jTextField10.getText()+"',bus_no='"+jTextField9.getText()+"',cus_name='"+jTextField8.getText()+"',Date='"+jTextField7.getText()+"',amount='"+jTextField6.getText()+"' where _ID='"+jTextField8.getText()+"' ");
I am trying to write a function for this button. I want to be able to pass it a textfield value and be able to go into my database to retrieve some information.....
Can somebody explain to me what is going on and provide me a solution to this madness?
Thank you all xD
I keep running into this stupid problem:
ACTION1 createdoracle.jdbc.driver.T4CConnection#484845aa
Exception:java.sql.SQLSyntaxErrorException: ORA-00904: "ART": invalid identifier
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//CLASS TYPE
//LIST ALL OFFERED CLASSES AND REVENUE
try{
String classtype = jTextField1.getText().trim();
if(classtype.equals("")){
JOptionPane.showMessageDialog(this, "Sorry Wrong input.... Please try again....");
}
else if(classtype != ""){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(
"jdbc:oracle:thin:#fourier.cs.iit.edu:1521:orcl",
"usr","pwd");
Statement stmt = conn.createStatement();
System.out.println("ACTION1 created"+conn+"\n\n");
String ct = jTextField1.getText().trim();
//String aa = "SELECT * FROM CLASS WHERE TYPE="+classtype;
//System.out.println(aa);
ResultSet rset = stmt.executeQuery("SELECT * FROM CLASS WHERE TYPE="+ct);
while (rset.next()) {
System.out.println(rset.getString("TITLE") + " ");
}
JOptionPane.showMessageDialog(this, "Class Type: "+classtype);
stmt.close();
conn.close();
System.out.println("Connection Closed");
}
catch(Exception sqle){
System.out.println("\nException:"+sqle);
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(this, "Please Retry input....", "Error", JOptionPane.ERROR_MESSAGE);
}
}
Let me guess ... does the ct String start with "ART" (or some variation)?
If so, the problem is that SQL requires quotes around string literals. Your query probably looks to Oracle something like this:
SELECT * FROM CLASS WHERE TYPE=Art of War
but it should look like
SELECT * FROM CLASS WHERE TYPE='Art of War'
There are two ways to fix this:
Assemble the query with quote characters around ct.
Write the query as "SELECT * FROM CLASS WHERE TYPE=?", use a PreparedStatement instead of a Statement and use the setString method to supply the parameter value.
If done properly, the second approach is both more secure and more efficient. (The problem with string-bashing the query and using Statement is that you are potentially making yourself vulnerable to SQL injection attacks.)
You're passing the value as part of the query, and the string concatenation you're doing makes the SQL into:
SELECT * FROM CLASS WHERE TYPE=ART
(where ART is the value of ct from the textfield) so it's trying to find a column on the table called ART. At an absolute minimum you need to quote the string:
ResultSet rset = stmt.executeQuery("SELECT * FROM CLASS WHERE TYPE='" + ct + "'");
But really don't do this; as #Andreas_D says you're leaving yourself open to SQL injection. Always use prepared statements and bind variables:
String sql = "SELECT * FROM CLASS WHERE TYPE=?";
PrepareStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, ct);
ResultSet rset = stmt.executeQuery();