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 8603815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:24:46+00:00 2026-06-12T02:24:46+00:00

Please have a look at the following code DataBaseConnector.java import java.sql.*; import javax.swing.*; public

  • 0

Please have a look at the following code

DataBaseConnector.java

 import java.sql.*;
 import javax.swing.*;

    public class DataBaseConnector
    {
        private Connection con;

        public DataBaseConnector()
        {

        }

        private boolean createConnection()
        {
            try
            {
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                con = DriverManager.getConnection("jdbc:derby://localhost:1527/contact;create=true","yohanrw","knight");
            }
            catch(Exception e)
            {      
                System.out.println("Error getConnection");
                e.printStackTrace();
                JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
                return false;
            }
            return true;   
        }

        private void closeConnection()
        {
            try
            {
                con.close();
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
            }
        }


        public void insertData(int id, String firstName, String lastName)
        {
            createConnection();
            try
            {
                PreparedStatement ps = con.prepareStatement("insert into APP.FRIENDS values(?,?,?)");
                ps.setInt(1, id);
                ps.setString(2, firstName);
                ps.setString(3, lastName);

                int result = ps.executeUpdate();

                if(result>0)
                {
                    JOptionPane.showMessageDialog(null,"Data Inserted");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Something Happened");
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
            }
            finally
            {
                closeConnection();
            }
        }

        public void viewData()
        {
            createConnection();

            try
            {
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("select * from APP.FRIENDS");

                StringBuffer sb = new StringBuffer("");

                while(rs.next())
                {
                    sb.append(rs.getInt(1)+"\n");
                    sb.append(rs.getString(2)+"\n");
                    sb.append(rs.getString(3)+"\n");


                }

                JOptionPane.showMessageDialog(null,sb);
            }
            catch(Exception e)
            {



            }
        }


    }

DatabaseUI

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class DatabaseUI extends JFrame
{
    private JLabel firstName, id, lastName;
    private JTextField idTxt, firstNameTxt, lastNameTxt;
    private JButton ok, view;

    public DatabaseUI()
    {
     firstName = new JLabel("First Name: ");
     lastName = new JLabel("Last Name: ");
     id = new JLabel("ID: ");

     firstNameTxt = new JTextField(10);
     lastNameTxt = new JTextField(10);
     idTxt = new JTextField(10);

     ok = new JButton("ADD");
     ok.addActionListener(new OKAction());
     view = new JButton("View");
     view.addActionListener(new ViewAction());

     JPanel centerPanel = new JPanel();
     centerPanel.setLayout(new GridLayout(4,2));
     centerPanel.add(id);
     centerPanel.add(idTxt);
     centerPanel.add(firstName);
     centerPanel.add(firstNameTxt);
     centerPanel.add(lastName);
     centerPanel.add(lastNameTxt);
     centerPanel.add(view);
     centerPanel.add(ok);

     getContentPane().add(centerPanel,"Center");


     this.pack();
     this.setVisible(true);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private class OKAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            DataBaseConnector db = new DataBaseConnector();

            int id = Integer.parseInt(idTxt.getText());

            db.insertData(id, firstNameTxt.getText().trim(), lastNameTxt.getText().trim());
        }
    }

    private class ViewAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            DataBaseConnector db = new DataBaseConnector();

            db.viewData();
        }
    }



    public static void main(String[]args)
    {
        new DatabaseUI();
    }
}

In this case, I need to start the derby manually (I am using NetBeans) by right clicking clicking on database node > start server. This is an embedded database, which means I am taking this from one machine to another and willing to start just by double clicking on the jar file, and not configuring database in each and every machine and starting them manually. But, if I didn’t start the database manually, I get an error

java.sql.SQLNonTransientConnectionException: java.net.ConnectException
: Error connecting to server localhost on port 1527 with message
Connection refused: connect.

No matter even inside NetBeans, if I didn’t start it manually, the error comes. How I can start the Derby inside my program, without manually starting it? I have tried some ways like “create=true” parameter, NetworkServer.start(), but no good. However I am not sure whether I did it correctly.

  • 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-12T02:24:47+00:00Added an answer on June 12, 2026 at 2:24 am

    This is a embedded database, which means I am taking this from one machine to another and willing to start just by double clicking on the jar file,

    In the case of derby, an embedded database means that the database runs in the JVM and writes to the file system. Which implies that you can move the jar file around like you want, but if you use an embedded database, then each machine that you run the program on will have its own separate database, and one and only one JVM can use that database at a time. Is that what you want?

    If so, the problem is the URL that the program uses. "jdbc:derby://localhost:1527/contact;create=true" is a URL that tells DriverManager to connect to a remote database. It doesn’t matter that program loads the embedded driver first.

    An embedded URL in Derby looks something like this. jdbc:derby:bar;create=true which will use an embedded database in the bar directory off of the Derby system home or current working directory. For more information on embedded urls, see here connecting to a file-based derby database.

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

Sidebar

Related Questions

Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import javax.swing.JTable; import javax.swing.*;
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue;

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.