I tried tutorial on youtube
I create the delete button in jframe java, then when I run an error occurs
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if (!jtextId.getText().equals(""))
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHRRE id = ?");
int id=Integer.parseInt(jtextId.getText());
ps.setInt(1, id);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Delete");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Produt Not Deleted");
}
}
else{
JOptionPane.showMessageDialog(null, "Enter Product");
}
}
On the output error is in ps.executeUpdate();
What should I fix??
If the code you pasted is your actual code, the error come from this line:
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHRRE id = ?");
You have a typo error in your SQL, replace by:
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHERE id = ?");
Related
I am getting this error when I run my code
Below is the code I have used:
String sql="SELECT * FROM PERSONS WHERE PERSONJOB='ADMIN'";
Statement stmt=null;
ResultSet rs=null;
try
{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
While(rs.next())
{
String name=rs.getString(1);
long id=rs.getLong(2);
System.out.println(name);
System.out.println(id);
}
}
catch(Exception e)
{
throw e;
}
finally
{
rs.close();
stmt.close();
}
I want to reuse the connection, so I didn't close the connection.
After I closed the ResultSet and Statement, I am getting the "maximum open cursors exceeded" error.
Anyone please help me to solve this error.
My guess is that the close() of your ResultSet is failing, which would result in multiple open cursors and eventually hit the max configured open cursor count. Could you modify your code to:
Statement stmt = conn.createStatement();
try
{
ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONS WHERE PERSONJOB = 'ADMIN'");
try
{
while ( rs.next() )
{
String name = rs.getString(1);
long id = rs.getLong(2);
System.out.println(name);
System.out.println(id);
}
}
finally
{
try
{
rs.close();
}
catch (Exception ignore) { }
}
}
finally
{
try
{
stmt.close();
}
catch (Exception ignore) { }
}
EDIT: will be good to also clean up the already opened cursors with a combination of:
SELECT oc.user_name, oc.sql_text, s.SID, s.SERIAL#
FROM v$open_cursor oc
, v$session s
WHERE oc.sid = s.sid
EXEC SYS.KILL_SESSION(xxx,xxxxx);
or restart the DB.
I am using netbeans 8.2 with xxamp. mysql connector.jar is 8.0.18.
It doesn't show any errors but whenever I press the button, the value from jUsername isn't inserted and instead the exception in the catch is shown. Please help.
private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String query = "INSERT INTO 'score' ('Name') VALUES (?)";
con = DriverManager.getConnection("jdbc:mysql://localhost/userscore", "root", "");
pst = con.prepareStatement(query);
pst.setString(1, jUsername.getText());
DifficultyPanel dpanel =new DifficultyPanel();
dpanel.setVisible(true);
this.dispose();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Please Enter Your Username To Continue");
}
}
Have a error, already added a jar rs2xml.jar but when project clean and build show this error- error: cannot find symbol
jTable1.setModel(DbUtils.resultSetToTableModel(rs)); don't understand that, please help me.
private void productCodeKeyReleased(java.awt.event.KeyEvent evt) {
String tf=productCode.getText();
PreparedStatement ps = null;
ResultSet rs = null;
Result rt = null;
Connection con = getCon();
try {
if(tf.matches("^[0-9]+$")){
String query = "SELECT * FROM product WHERE ProductCode="+tf;
ps = con.prepareStatement(query);
rs=ps.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));//error show here
}else{
String query1 = "SELECT * FROM product WHERE ProductName LIKE'%"+tf+"%'";
ps = con.prepareStatement(query1);
rs=ps.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));// error show here
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(ps!=null){
rs.close();
ps.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Remove
private Object DBUtils
And import instead
net.proteanit.sql.DbUtils
private void button_saveandsubmitnowActionPerformed(java.awt.event.ActionEvent evt) {
con = JavaConnectDB.ConnectDB();
try{
String sql="select MISSINGPERSON_SEQUENCE.nextval from DUAL";
pst = (OraclePreparedStatement) con.prepareStatement(sql);
rs = (OracleResultSet) pst.executeQuery();
JOptionPane.showMessageDialog(null, "\nCase number: "+rs.getString(1));
rs = (OracleResultSet) pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "the details have been added successfully!!!");
con.close();
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "enter all fields appropriately first!!!");
}
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, "An error occured. try again later !!!");
}
}
When I perform any action it works in database but suddenly it shows an error of Database is Locked!
Suppose this is the actionPerformed on one button:
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
//Sahil_Computers.ConnecrDb(); is the database connecting method!
conn = Sahil_Computers.ConnecrDb();
try{
String sql = "insert into dailyExp (Sr,Description,Amount) values (?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, txt_srE.getText());
pst.setString(2, txt_desE.getText());
pst.setString(3, txt_rsE.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Saved!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
update_table_exp();
}
and then again when I try to perform another action just like:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
conn = Sahil_Computers.ConnecrDb();
String sql = "delete from dailySale where Sr=?";
try{
pst = conn.prepareStatement(sql);
pst.setString(1, txt_sr1.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Deleted!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
update_table_sale();
}
or action like this one:
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
conn = Sahil_Computers.ConnecrDb();
try{
String sum = null, sub= null;
String subto = null;
int sum1, sub1, subto1;
String sql = "select sum(Debit) from dailySale";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
sum = rs.getString("sum(Debit)");
txt_tsale.setText(sum);
}
sql = "select sum(Amount) from dailyExp";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
sub = rs.getString("sum(Amount)");
txt_texp.setText(sub);
}
sum1 = Integer.parseInt(sum);
sub1 = Integer.parseInt(sub);
subto1 = sum1 - sub1;
subto = Integer.toString(subto1);
txt_tsub.setText(subto);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}}}
Then it shows database is locked!
You must close any open connection before open a new one. You're opening a new connection conn = Sahil_Computers.ConnecrDb(); every time a button is pressed but you never close it. Add conn.close(); to your finally blocks.
Some off-topic concern: use PreparedStatement#executeUpdate() instead PreparedStatement#execute() when you need to execute an INSERT/UPDATE/DELETE statement.