Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:22:21+00:00 2026-06-08T23:22:21+00:00

I need to insert some data into a table. When program executes to insert,

  • 0

I need to insert some data into a table. When program executes to insert, there may arise an error because I haven’t created the table. I would like to create that table in a catch block and return to try block. So whenever the table is deleted or happens to delete, the try catch will build the table.

I have written some code for it, but it fails.

import java.sql.*;

/**
 *
 * @author JOJO
 */
public class ConnectDB {
  static Connection conn;
   static String driver = "org.apache.derby.jdbc.ClientDriver";
   static String connectionURL = "jdbc:derby://localhost:1527/Libraryprj;user=scott;password=tiger";

  public void AddBookDB(String bookName) throws SQLException {

    String createString = "CREATE TABLE BOOKLEDGER (BOOKNAME VARCHAR(50) NOT NULL)";
    try {
      Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
    try
        {
            conn = DriverManager.getConnection(connectionURL);
            Statement stmt = conn.createStatement();
            boolean execute = stmt.execute("insert into BOOKLEDGER values ('" + bookName + "')");
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            Statement stmt = (Statement) conn.createStatement();
            stmt.executeUpdate(createString);

            sqlExcept.printStackTrace();
        }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:22:23+00:00Added an answer on June 8, 2026 at 11:22 pm

    From my point of view it is better to proceed in another way(use control structure instead of try-catch), so you can test before if the table exist in this way :

    DatabaseMetaData md = conn.getMetaData();
    ResultSet rs = md.getTables(null, null, "table_name", null);
    if (!rs.next()) {
      //Table not Exist, let's create it 
    }
    //Insert data into the table ...
    

    It is a bad practice using try-catch in a similar case, try catch construct should be used only to catch exceptional situation, not in place of a control structure …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an SQL query to insert some data into a table
I need to insert some data in bulk into an azure storage table. As
Need to insert selected text on the page into textarea. There must be some
After inserting new data into a table, I need to select some of the
I want to insert some data into a table associated with dates for the
I have a form I need to insert some information into one table. Then
I need to insert some data into a mysql database. Db is connected and
I am inserting some data into a MySQL table using CodeIgniter. Because I am
I need to write some code to insert around 3 million rows of data.
I need some help to insert comma separates strings into multiple columns into db.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.