• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Single Application but Multiple Connection problem

 
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All


Problem 1
==========

Whenever i run my application, Login form appears. After clicking login button it shows Connection Established.

Then it send me to Main form.

Main form contents number of buttons. When i click company form. At form load it shows "Connection Established...."

When i click Query All Button it again shows Connection Established..

Plz guide me where i m wrong???

"Is this multiple connection are because of my declaration of object
dbConnection cnn = new dbConnection();
in evry form".

If yes then plz guide me wht i ve 2 do 2 ve only one connection for all my application.


Problem 2
============
//////////////////////////////////
UTIL CLASS
//////////////////////////////////

i ve another util class having 2 object of same dbConnection();
dbConnection cnn = new dbConnection();
dbConnection cnnDetail = new dbConnection();

When ever i make object its always shows Connection Established two times.
If i m making object of util class at local, it also shows Connection Established two times.


////////////////////////////////////////
dbConnection.class
////////////////////////////////////////

import java.sql.*;


class dbConnection {
public Connection Conn ;
public Statement st,ss;
public ResultSet rs;
public String strUserID;
Statement ss=null;

/** Creates a new instance of Connection */
public dbConnection() {

try{
Class.forName("org.postgresql.Driver");
Conn = DriverManager.getConnection("jdbc ostgresql://192.168.0.1:5432/mydb","postgres","postgres");
st = Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
System.out.println("Connection Established.........");
}
catch(Exception e){
System.out.println("Connection mein panga hai bhai "+ e );
}
}

public ResultSet getConnection (String str) throws SQLException
{
ResultSet rs = st.executeQuery(str);
return rs;
}
public String setUserID(String str) throws SQLException
{ String cc;
strUserID = str;
cc = strUserID;
return cc;

}
}


///////////////////////////////////
HERE IS LOGIN CLASS
//////////////////////////////////

import javax.swing.*;
import java.awt.event.WindowEvent.*;
import java.sql.*;
public class Login extends javax.swing.JFrame {
public ResultSet rs ;
dbConnection cnn = new dbConnection();
public String strUserID;
/** Creates new form Login */
public Login() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtUserID = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtPassword = new javax.swing.JPasswordField();
lblStatus = new javax.swing.JLabel();
login = new javax.swing.JButton();
exit = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();

getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Login");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jLabel1.setFont(new java.awt.Font("Default", 1, 12));
jLabel1.setText("User ID");
getContentPane().add(jLabel1);
jLabel1.setBounds(40, 50, 60, 15);

txtUserID.setNextFocusableComponent(txtPassword);
txtUserID.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
txtUserIDFocusLost(evt);
}
});
txtUserID.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtUserIDKeyPressed(evt);
}
});

getContentPane().add(txtUserID);
txtUserID.setBounds(120, 50, 120, 19);

jLabel2.setFont(new java.awt.Font("Default", 1, 12));
jLabel2.setText("Password");
getContentPane().add(jLabel2);
jLabel2.setBounds(40, 80, 70, 15);

txtPassword.setBackground(new java.awt.Color(255, 204, 204));
txtPassword.setNextFocusableComponent(login);
getContentPane().add(txtPassword);
txtPassword.setBounds(120, 80, 120, 19);

lblStatus.setForeground(new java.awt.Color(255, 0, 0));
getContentPane().add(lblStatus);
lblStatus.setBounds(90, 180, 180, 20);

login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/blueHome.png")));
login.setMnemonic('g');
login.setText("Go");
login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});

getContentPane().add(login);
login.setBounds(60, 130, 90, 32);

exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/redstop.png")));
exit.setMnemonic('x');
exit.setText("Exit");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});

getContentPane().add(exit);
exit.setBounds(150, 130, 90, 32);

jLabel6.setFont(new java.awt.Font("Default", 1, 12));
jLabel6.setLabelFor(lblStatus);
jLabel6.setText("Status:");
getContentPane().add(jLabel6);
jLabel6.setBounds(32, 180, 50, 15);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-331)/2, (screenSize.height-242)/2, 331, 242);
}

try{ System.out.println("Going to Check User");
String sql = "Select userid, pwd from user_manager where userid = '" + txtUserID.getText() + "' and pwd = '" + txtPassword.getText() + "'";
lblStatus.setText("Going Now");
////////////////

Statement st = cnn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
rs = cnn.getConnection(sql);
if (rs.next()==false)
{
lblStatus.setText("No record found");
}
else{
rs.last();
if (rs.getString(1) == null)
{
System.out.println("No defined user is found");
lblStatus.setText("Invalid User ID ");
}
else
{
cnn.strUserID = txtUserID.getText();
Company_Year_Selection cd = new Company_Year_Selection();
cd.show();
setVisible(false);
cd.txtUserId.setText(txtUserID.getText());

}
}
///////////////
}
catch(Exception e){}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Login().show();
}

// Variables declaration - do not modify
private javax.swing.JButton exit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel lblStatus;
private javax.swing.JButton login;
private javax.swing.JPasswordField txtPassword;
public javax.swing.JTextField txtUserID;
// End of variables declaration
}






//////////////////////////////
Here is a UNIT SETUP FORM...
///////////////////////////////


import java.sql.*;
import javax.swing.*;
import java.util.Date;
import java.util.Calendar;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;


public class Unit_Setup extends javax.swing.JFrame {
dbConnection cnn= new dbConnection();
Statement st,ss;

ResultSet rs=null;
String emode = "N";
public String ID ="unit_id" ; //Table Serial No or ID No
public String Title="unit_name";//Table Title or Name
public String Remarks="remarks";//Table Remarks
public String TableName="unit";//Database Table Name

public String UID = "uid";
public String sysDate = "sysdate";
public String sysTime ="systime";
/** Creates new form frmDept */
public Unit_Setup() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
txtID = new javax.swing.JTextPane();
txtTitle = new javax.swing.JTextPane();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtRemarks = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
lblUserID = new javax.swing.JLabel();
lblStatus = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
btnSearch = new java.awt.Button();
jLabel6 = new javax.swing.JLabel();
lblDate = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
lblTime = new javax.swing.JLabel();
lblCurrentUser = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
btnQueryAll = new javax.swing.JButton();
btnLast = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
btnPrevious = new javax.swing.JButton();
btnFirst = new javax.swing.JButton();
btnPrint = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();

getContentPane().setLayout(null);

setTitle("Unit Setup");
setState(-1);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
addWindowStateListener(new java.awt.event.WindowStateListener() {
public void windowStateChanged(java.awt.event.WindowEvent evt) {
formWindowStateChanged(evt);
}
});

txtID.setNextFocusableComponent(txtTitle);
getContentPane().add(txtID);
txtID.setBounds(130, 20, 70, 20);

txtTitle.setNextFocusableComponent(txtRemarks);
txtTitle.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
keyEnter(evt);
}
});

getContentPane().add(txtTitle);
txtTitle.setBounds(130, 50, 370, 21);

jLabel1.setText("ID");
getContentPane().add(jLabel1);
jLabel1.setBounds(40, 20, 50, 15);

jLabel2.setText("Title");
getContentPane().add(jLabel2);
jLabel2.setBounds(40, 50, 80, 15);

jLabel3.setText("Remarks");
getContentPane().add(jLabel3);
jLabel3.setBounds(40, 80, 70, 15);

txtRemarks.setFocusCycleRoot(true);
txtRemarks.setNextFocusableComponent(btnSave);
txtRemarks.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtRemarksKeyEnter(evt);
}
});

getContentPane().add(txtRemarks);
txtRemarks.setBounds(130, 80, 440, 20);

jLabel4.setText("User");
getContentPane().add(jLabel4);
jLabel4.setBounds(810, 40, 40, 20);

lblUserID.setBackground(new java.awt.Color(204, 204, 255));
lblUserID.setForeground(new java.awt.Color(51, 0, 51));
lblUserID.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 51, 51)));
getContentPane().add(lblUserID);
lblUserID.setBounds(850, 40, 100, 20);

lblStatus.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
getContentPane().add(lblStatus);
lblStatus.setBounds(60, 210, 720, 20);

jLabel5.setText("Status:");
getContentPane().add(jLabel5);
jLabel5.setBounds(10, 210, 42, 20);

btnSearch.setLabel("Search");
btnSearch.setVisible(false);
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});

getContentPane().add(btnSearch);
btnSearch.setBounds(515, 50, 54, 23);

jLabel6.setText("Date");
getContentPane().add(jLabel6);
jLabel6.setBounds(810, 60, 40, 20);

lblDate.setForeground(new java.awt.Color(51, 0, 51));
lblDate.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 51, 51)));
getContentPane().add(lblDate);
lblDate.setBounds(850, 60, 100, 20);

jLabel8.setText("Time");
getContentPane().add(jLabel8);
jLabel8.setBounds(810, 80, 40, 20);

lblTime.setBackground(new java.awt.Color(255, 0, 0));
lblTime.setForeground(new java.awt.Color(51, 0, 51));
lblTime.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 51, 51)));
getContentPane().add(lblTime);
lblTime.setBounds(850, 80, 100, 20);

lblCurrentUser.setBorder(new javax.swing.border.EtchedBorder());
getContentPane().add(lblCurrentUser);
lblCurrentUser.setBounds(850, 10, 100, 20);

jLabel9.setText("Current User");
getContentPane().add(jLabel9);
jLabel9.setBounds(760, 10, 90, 15);

btnQueryAll.setFont(new java.awt.Font("SansSerif", 0, 12));
btnQueryAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/bluemail_find.png")));
btnQueryAll.setMnemonic('h');
btnQueryAll.setText("Show All Data");
btnQueryAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnQueryAllActionPerformed(evt);
}
});

getContentPane().add(btnQueryAll);
btnQueryAll.setBounds(810, 120, 138, 32);

btnLast.setFont(new java.awt.Font("Serif", 0, 10));
btnLast.setForeground(new java.awt.Color(0, 0, 204));
btnLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/greenLast.png")));
btnLast.setMnemonic('t');
btnLast.setText("Last");
btnLast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLastActionPerformed(evt);
}
});

getContentPane().add(btnLast);
btnLast.setBounds(870, 160, 80, 30);

btnNext.setFont(new java.awt.Font("Serif", 0, 10));
btnNext.setForeground(new java.awt.Color(0, 0, 204));
btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/greenNext.png")));
btnNext.setMnemonic('n');
btnNext.setText("Next");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});

getContentPane().add(btnNext);
btnNext.setBounds(780, 160, 90, 30);

btnPrevious.setFont(new java.awt.Font("Serif", 0, 10));
btnPrevious.setForeground(new java.awt.Color(0, 0, 204));
btnPrevious.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/greenprevious.png")));
btnPrevious.setMnemonic('r');
btnPrevious.setText("Previous");
btnPrevious.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPreviousActionPerformed(evt);
}
});

getContentPane().add(btnPrevious);
btnPrevious.setBounds(680, 160, 100, 30);

btnFirst.setFont(new java.awt.Font("Serif", 0, 10));
btnFirst.setForeground(new java.awt.Color(0, 0, 204));
btnFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/greenFirst.png")));
btnFirst.setMnemonic('f');
btnFirst.setText("First");
btnFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFirstActionPerformed(evt);
}
});

getContentPane().add(btnFirst);
btnFirst.setBounds(590, 160, 90, 30);

btnPrint.setFont(new java.awt.Font("Serif", 0, 10));
btnPrint.setForeground(new java.awt.Color(0, 0, 204));
btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/bluefileprint.png")));
btnPrint.setMnemonic('p');
btnPrint.setText("Print");
btnPrint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPrintActionPerformed(evt);
}
});

getContentPane().add(btnPrint);
btnPrint.setBounds(510, 160, 83, 30);

btnExit.setFont(new java.awt.Font("Serif", 0, 10));
btnExit.setForeground(new java.awt.Color(0, 0, 204));
btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/redDelete.png")));
btnExit.setMnemonic('X');
btnExit.setText("Exit");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});

getContentPane().add(btnExit);
btnExit.setBounds(430, 160, 80, 30);

btnCancel.setFont(new java.awt.Font("Serif", 0, 10));
btnCancel.setForeground(new java.awt.Color(0, 0, 204));
btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/blueRoundreload.png")));
btnCancel.setMnemonic('C');
btnCancel.setText("Cancel");
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});

getContentPane().add(btnCancel);
btnCancel.setBounds(340, 160, 93, 30);

btnDelete.setFont(new java.awt.Font("Serif", 0, 10));
btnDelete.setForeground(new java.awt.Color(0, 0, 204));
btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/whiteeditdelete.png")));
btnDelete.setMnemonic('D');
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});

getContentPane().add(btnDelete);
btnDelete.setBounds(250, 160, 90, 30);

btnSave.setFont(new java.awt.Font("Serif", 0, 10));
btnSave.setForeground(new java.awt.Color(0, 0, 204));
btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/bluefilesaveas.png")));
btnSave.setMnemonic('S');
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});

getContentPane().add(btnSave);
btnSave.setBounds(170, 160, 80, 30);

btnEdit.setFont(new java.awt.Font("Serif", 0, 10));
btnEdit.setForeground(new java.awt.Color(0, 0, 204));
btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/blueedit.png")));
btnEdit.setMnemonic('e');
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});

getContentPane().add(btnEdit);
btnEdit.setBounds(90, 160, 80, 30);

btnAdd.setFont(new java.awt.Font("Serif", 0, 10));
btnAdd.setForeground(new java.awt.Color(0, 0, 204));
btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/bluefileopen.png")));
btnAdd.setMnemonic('A');
btnAdd.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
btnAdd.setLabel("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});

getContentPane().add(btnAdd);
btnAdd.setBounds(10, 160, 80, 30);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-977)/2, (screenSize.height-282)/2, 977, 282);
}

private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnLast();
}

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnNext();
}

private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnPrevious();
}

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnFirst();
}

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnPrint();
}

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnExit();
}

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnCancel();
}

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnDelete();
}

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnSave();
}

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnEdit();
}

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnAdd();
}

private void btnQueryAllActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
func_btnQueryAll();
}

private void txtRemarksKeyEnter(java.awt.event.KeyEvent evt) {
// Add your handling code here:
txtRemarks.addKeyListener(new Unit_Setup.MyKeyListener());
}

private void formWindowStateChanged(java.awt.event.WindowEvent evt) {
// Add your handling code here:

}

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
try {
String sql="Select " + ID + ", " + Title + " " +
" , " + Remarks + ", " + UID + ", " + sysDate + ", " + sysTime + " from " +
" " + TableName + " where " + Title + " like " + txtTitle.getText() + " order by " + ID + "";
System.out.println(sql);
rs = cnn.st.executeQuery(sql);

displayRecord1();
}
catch (Exception e){
System.out.println("No data found in search");
}
}
private void displayRecord1(){
try{

if (rs.getString(1) == null)
{
txtID.setText("");
}
else
{
txtID.setText(rs.getString(1));
}
if (rs.getString(2) == null)
{
txtTitle.setText("");
}
else
{
txtTitle.setText(rs.getString(2));
}
if (rs.getString(3) == null)
{
txtRemarks.setText("");
}
else
{
txtRemarks.setText(rs.getString(3));
}
if (rs.getString(4) == null)
{
lblUserID.setText("");
}
else
{
lblUserID.setText(rs.getString(4));
}
if (rs.getString(5) == null)
{
lblDate.setText("");
}
else
{
lblDate.setText(rs.getString(5));
}
if (rs.getString(6) == null)
{
lblTime.setText("");
}
else
{
lblTime.setText(rs.getString(6));
}
} catch (Exception e) {


}

}
private void func_btnAdd()
{
lblUserID.setText("anand");
emode = "A";
txtID.setText("");
txtTitle.setText("");
txtRemarks.setText("");
getMaxValue();
lblStatus.setText("Adding New Record");
btnCancel.enable(false);
btnAdd.enable(false);
btnEdit.enable(false);
btnSave.enable(true);
btnDelete.enable(false);
btnCancel.enable(true);
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
myDate();
}

private void func_btnEdit()
{
lblStatus.setText("Editing Record");
emode = "E";
btnAdd.enable(false);
btnEdit.enable(false);
btnSave.enable(true);
btnDelete.enable(false);
btnCancel.enable(true);
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
}

private void func_btnDelete()
{
lblStatus.setText("Delete Record");
deleteRecord();
btnAdd.enable(true);
btnEdit.enable(true);
btnSave.enable(true);
btnDelete.enable(true);
btnCancel.enable(true);
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
func_btnQueryAll();
}

private void func_btnSave()
{
if (emode =="A") {
myDate();
lblStatus.setText("Save Record");
InsertRecord();}
else if (emode == "E")
{
lblStatus.setText("Edit Record Saved");
updateRecord();
}
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
btnAdd.enable(true);
btnEdit.enable(true);
btnSave.enable(true);
btnDelete.enable(true);
btnCancel.enable(false);
func_btnQueryAll();
}

private void func_btnCancel()
{
if (emode =="A") {
lblStatus.setText("Transaction Cancelled");
btnAdd.enable(true);
btnEdit.enable(true);
btnSave.enable(true);
btnDelete.enable(true);
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
emode = "N"; //Normal Mode
txtID.setText("");
txtTitle.setText("");
txtRemarks.setText("");
func_btnQueryAll();
}
else if (emode == "E")
{
lblStatus.setText("Transaction Cancelled");
btnAdd.enable(true);
btnEdit.enable(true);
btnSave.enable(true);
btnDelete.enable(true);
btnFirst.enable(false);
btnPrevious.enable(false);
btnNext.enable(false);
btnLast.enable(false);
emode = "N"; //Normal Mode
txtID.setText("");
txtTitle.setText("");
txtRemarks.setText("");
func_btnQueryAll();
}
}

private void func_btnExit()
{
setVisible(false);
}

private void func_btnFirst()
{
try{
if (rs.first() == false)
{
rs.previous();
displayRecord1();
}
else
{
rs.first();
displayRecord1();
}
}
catch (Exception e) {
lblStatus.setText("No First Record found");
}
}

private void func_btnPrevious()
{
try{
if (rs.previous() ==false)
{
rs.first();
displayRecord1();
}
else
{
rs.next();
rs.previous();
displayRecord1();
}
}
catch (Exception e) {
lblStatus.setText("No Previous Record found");
}
}

private void func_btnNext()
{
try{
if (rs.next() ==false)
{
rs.previous();
displayRecord1();
}
else
{
rs.previous();
rs.next();
displayRecord1();
}
}
catch (Exception e) {
lblStatus.setText("No Next Record found");
}
}

private void func_btnLast()
{
try{
rs.last();
displayRecord1();
}

catch (Exception e)
{
lblStatus.setText("No Last Record found");
}
}

private void func_btnQueryAll()
{
try{
emode = "Q";
//System.out.println("Query button click");
String sql="Select " + ID + ", " + Title + "" +
"," + Remarks + "," + UID + "," + sysDate + "," + sysTime + " from " +
" " + TableName + " order by " + ID + "";

// System.out.println(sql);
st = cnn.Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
rs =cnn.getConnection(sql);

if (rs.next()==false)
{
lblStatus.setText("No record found");
}
rs.last();
displayRecord1();
}
catch(Exception e) {

lblStatus.setText("No data found" +e);
}
btnFirst.enable(true);
btnPrevious.enable(true);
btnNext.enable(true);
btnLast.enable(true);
}

private void func_btnPrint()
{
lblStatus.setText("Printing feature is closed temporary");
}



private void updateRecord(){
if (emode =="E") {
try{
String s = " update " + TableName + " set "+
" "+ Title + " = '" + txtTitle.getText() + "', " + Remarks + " = '" + txtRemarks.getText() + "', " + UID + " = '" + lblUserID.getText() + "' , " + sysDate + " = '" + lblDate.getText() + "' , " + sysTime + " = '" + lblTime.getText() + "' Where " + ID + " = '" + txtID.getText() + "' ";
//System.out.println(s);
int a = cnn.st.executeUpdate(s);
lblStatus.setText("Data Updated Successfully");//
s = "";
}
catch(SQLException e){
lblStatus.setText("Error in Saving Data.....");
}
}
else {
lblStatus.setText("System is not in editing mode");
}

}
private void InsertRecord(){
if (emode == "A") {
try{
getMaxValue();
String s = "insert into "+ TableName +" values ( " + txtID.getText() + ", '" + txtTitle.getText() + "', '" + txtRemarks.getText() + "', '" + lblUserID.getText() + "' , '" + lblDate.getText() + "' , '" + lblTime.getText() + "' )";
//System.out.println(s);
int a = cnn.st.executeUpdate(s);
lblStatus.setText("Data Saved Successfully");//
s = "";
}
catch(SQLException e){
lblStatus.setText("Error in Saving Data.....");
}
}
else {
lblStatus.setText("Mode is not in Addition");
}

}
private void deleteRecord(){
try{
String s = "Delete from " + TableName + " where " + ID + " = '" + txtID.getText() + "'";
int a = cnn.st.executeUpdate(s);
lblStatus.setText("Delete record successfully");
}
catch (Exception e)
{
lblStatus.setText("Error in deleting Data....." + e);
}
}
private void keyEnter(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if (txtTitle.isFocusable())
{
txtTitle.addKeyListener(new Unit_Setup.MyKeyListener());
}



}
public class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt)
{
if (evt.getKeyCode() == KeyEvent.VK_ENTER)
{
// System.out.println("Enter is pressed");
if ( txtTitle.isFocusable())
{
txtRemarks.nextFocus();
}

else if (txtRemarks.isFocusable())
{
//txtRemarks.nextFocus();
func_btnSave();
}

}
}

}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Unit_Setup().show();
}

private void getMaxValue(){
try{
String maxN = "Select " + ID + " + 1 " + " from " + TableName + " order by " + ID + " desc";
//System.out.println(maxN);
ResultSet rs = cnn.getConnection(maxN);
if (rs.next() ==false)
{
lblStatus.setText("Data Not Found");
}
txtID.setText(rs.getString(1));
rs.close();
}
catch (Exception e)
{
lblStatus.setText("Max value not found");
}
}

// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnFirst;
private javax.swing.JButton btnLast;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrevious;
private javax.swing.JButton btnPrint;
private javax.swing.JButton btnQueryAll;
private javax.swing.JButton btnSave;
private java.awt.Button btnSearch;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
public static javax.swing.JLabel lblCurrentUser;
private javax.swing.JLabel lblDate;
private javax.swing.JLabel lblStatus;
private javax.swing.JLabel lblTime;
private javax.swing.JLabel lblUserID;
private javax.swing.JTextPane txtID;
private javax.swing.JTextField txtRemarks;
private javax.swing.JTextPane txtTitle;
// End of variables declaration
protected SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public void setDateFormat(SimpleDateFormat dateFormat) {
this.dateFormat = dateFormat;
//System.out.println( dateFormat );
}

public void myDate()
{
//System.out.println(dateFormat.format(new Date()));
lblDate.setText(dateFormat.format(new Date()));
myTime();
}
protected SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
public void setTimeFormat(SimpleDateFormat timeFormat) {
this.timeFormat = timeFormat;
//System.out.println( timeFormat );
}

public void myTime()
{
//System.out.println(timeFormat.format(new Date()));
lblTime.setText(timeFormat.format(new Date()));
}


}






Connection Established
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reg Problem#2, since your util class has two dbconnection objects, its obvious that, when you make an instance of the util class.. its child objects ie both the connections will be established.

Reg Problem#1 , sorry now i am not able to go through the entire code..
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic