SQLITE Database is locked in java (IDE NetBeans) - java

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.

Related

Keep particular data in a JTable from database

I want to show particular data that keep one by one data in jtable, but never show all data,
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
String txt = jTextField1.getText().toString();
Connection cn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
cn = Dataconnection.getConnection();
qr = "SELECT p.ProductCode,p.ProductName, p.ProductPrice FROM
customer.product p WHERE CONCAT(p.ProductCode, p.ProductName) LIKE '%"+txt+"%'";
ps=cn.prepareStatement(qr);
ps.setString(1, jTextField1.getText());
rs=ps.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
} catch (Exception e) {
}
}
}

how to get next value of sequence from database and show it in JOptionPane in netbeans using oracle 10g?

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 !!!");
}
}

Selecting a count from my database using Java and Netbeans

I have the following code:
try {
String sql = "SELECT COUNT(*) AS \"Jumlah\" FROM dokter";
ResultSet rs = connection.st.executeQuery(sql);
if(rs.next()){
abc = rs.getString("Jumlah").toString();
}
} catch (Exception e) {
System.out.println("\n Message: " + e.getMessage());
}
Why can't my ResultSet execute the given SQL?
Lose the alias, it's just an unnecessary complication. Just reference the ResultSet by the column's index:
try {
String sql = "SELECT COUNT(*) FROM dokter";
ResultSet rs = connection.st.executeQuery(sql);
if(rs.next()) {
abc = rs.getInt(1); // or getString(1) if you need it as a String
}
} catch (Exception e) {
System.out.println("\n Message: " + e.getMessage());
}
I suggest you use a PreparedStatement and a try-with-resources to close it (and your ResultSet). A count is not a String, and if you have a Connection connection then you might do something like
int count = 0;
try {
String sql = "SELECT COUNT(*) FROM dokter";
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
count = rs.getInt(1);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Save edited cell/s from JTable to database

Newbie to JTable. Just got in today.
So I am able to populate my data from database into a JTable.
I am currently stuck in saving the edited cell/s into the database.
I've read many articles. But I haven't found the one
Just a button then save!
Here's my code
public class DBEngine {
Connection con = IQConnection.getConnectionMgrInstance().getConnection();
PreparedStatement pstm = null;
ResultSet rs = null;
public Vector getEmployee() throws Exception{
Vector<Vector<String>> employeeVector = new Vector<Vector<String>>();
try {
if (con != null) {
String query = "SELECT * FROM OGG.TBLSAMPLE";
try {
pstm = con.prepareStatement(query);
rs = pstm.executeQuery();
while(rs.next()) {
Vector<String> employee = new Vector<String>();
employee.add(rs.getString(1)); //Empid
employee.add(rs.getString(2)); //name
employee.add(rs.getString(3)); //position
employee.add(rs.getString(4)); //department
employeeVector.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
//lbl_err1.setText("Error");
}
} catch (Exception e) {
e.printStackTrace();
//flag = false;
//lbl_err1.setText("Failed!");
} finally {
try{
if(rs != null){
rs.close();
}
}catch(Exception e){
}
try{
if(pstm != null){
pstm.close();
}
}catch(Exception e){
}
}
return employeeVector;
}
}
Thanks!
use preparedstatement like this way for inserting into DB
PreparedStatement pt=con.prepareStatement("insert into mytable values(?) ");
pt.setString(1,ID);
int result=pt.executeUpdate();
if you want to update then use this way
PreparedStatement pt=con.prepareStatement("update mytable set column_name=? ");
pt.setString(1,ID);
int result=pt.executeUpdate();

java - retrieving data from database and display using JcomboBox

I would like to be able to select a data from database and display it using the comboBox.
I have the following code but it does not display the data in the comboBox. I do realize there is codes missing to display the data and my SQL statement is not correct. I am just not sure what I can do any advise is appreciated.
try {
Statement st = db.con.createStatement();
ResultSet rs = st.executeQuery("SELECT Name, Size, Price FROM item WHERE Name=" + comboBox_1.getToolkit());
JOptionPane.showMessageDialog(frame, "displayed");
while (rs.next()) {
String name = rs.getString("Name");
String size = rs.getString("size");
String price = rs.getString("price");
textArea_Name.append(name);
textArea_size.append(size);
textArea_price.append(price);
comboBox_1.addItem(rs.getString("Name"));
comboBox_1.getSelectedItem();
}}
catch (SQLException e ) {
System.out.println("user not added");
e.printStackTrace();
}
}
});
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:driverName");
Statement st = conn.createStatement();
ResultSet r=st.executeQuery("select * from tableName");
while (r.next()) {
JComboBox.addItem(r.getString("Key_Coloumn_Name"));
}
conn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
Try this tutorial. It does exactly that.
http://www.roseindia.net/tutorial/java/swing/comboboxwithdatabaseValues.html
try {
Statement stmt = db.con.createStatement();
ResultSet rs = stmt5.executeQuery("select * from tbl_your_table");
while (rs.next()) {
String pat = rs.getString("name");
jComboBox.addItem(pat);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
con = DriverManager.getConnection("jdbc:mysql://:3306/database","user","password");
stat = con.createStatement();
ResultSet rs = stat.executeQuery("select name from student;");
while(rs.next()){
jComboBox1.addItem(rs.getString("name"));
}
rs.close();
stat.close();
con.close();
comb = new JComboBox();
DefaultComboBoxModel dftm = (DefaultComboBoxModel) comb.getModel();
ArrayList<stock_data> list = userLists();
row = new Object[1];
for (int i = 0; i < list.size(); i++) {
row[0] = list.get(i).get_item();
dftm.addElement(row);
// comb.setModel(dftm);
}
Then create your method and also remember you need a stock_data Class
private ArrayList<stock_data> userLists() {
try {
connection = localhost.dbConnector();
ArrayList<stock_data> usersList = new ArrayList();
try {
String query = "Select * from itemlist";
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
stock_data users;
while (rs.next()) {
// users = new stock_data(rs.getString("name"));
// usersList.add(users);
comb.addItem(rs.getString("name"));
}
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "blah blah");
e.printStackTrace();
}
return usersList;
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}

Categories