Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: jdbc integration with Gui to show user information retrieved from mysql

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jdbc integration with Gui to show user information retrieved from mysql

    gui Part


    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import java.awt.Dimension;
    import java.sql.SQLException;
    import java.util.Scanner;

    public class GUIpart extends JFrame implements ActionListener{


    String[] fields = {"Name: ", "Company: ", "Title: ", "Mobile: ", "Email: ", "URL: ", "Phone home 1: ", "Phone work 1: ", "Phone home 2: ", "Phone work 2: ", "Home address: ", "Work address: "};
    String[][] results;
    JButton submit, cancel, next, previous;

    JPanel outerPanel;

    JPanel tpanel;
    JPanel downpanel;
    JPanel toppanel;
    JPanel rightPanel;
    JPanel down2panel ;
    JPanel all2getherpanel;




    JMenu records;
    JMenu about;
    JMenu help;
    JMenu view;
    JMenuBar myBar;
    JSeparator sep;
    JMenuItem newRecord, uer, deleteRecord, exit, byName, byCompany, byCell;


    JLabel firstlabel,secondlabel,thirdlabel,fourthlabel,fift hlabel,sixthlabel,seventhlabel,eigthenthlabel,nine thlabel,tenthlabel,eleventhlabel,twelevthlabel;


    JTextField pagenum, recordnum, firsttext,secondtext,thirdtext,fourthtext,fifthtex t,sixthtext,seventhtext,eighthtext,ninehttext,tent htext,eleventhtext,twelevthtext;

    GUIpart(){

    setTitle("Address Book-ITE 409-Project");
    setSize(950,400);
    setLocation(210,110);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createGUI();

    }
    public void createGUI(){


    outerPanel= new JPanel();
    tpanel=new JPanel();
    toppanel= new JPanel();
    rightPanel= new JPanel();
    down2panel=new JPanel();
    all2getherpanel=new JPanel();



    all2getherpanel.setLayout(new BorderLayout());
    outerPanel.setLayout(new BorderLayout());


    tpanel.setLayout(new GridLayout(6,2,8,8));


    toppanel.setLayout(new BorderLayout());

    rightPanel.setLayout(null);
    down2panel.setLayout(new GridLayout(3,2));





    downpanel =new JPanel();
    previous=new JButton("Previous");
    previous.addActionListener(this);

    next=new JButton("Next");
    next.addActionListener(this);

    submit=new JButton("Submit");
    submit.addActionListener(this);

    cancel=new JButton("Cancel");
    cancel.addActionListener(this);

    downpanel.setLayout(new FlowLayout());

    pagenum= new JTextField(12);
    pagenum.addActionListener(this);

    recordnum=new JTextField(12);
    recordnum.addActionListener(this);

    downpanel.add( pagenum);
    downpanel.add(recordnum);



    downpanel.add(previous);
    downpanel.add(next);
    downpanel.add(submit);
    downpanel.add(cancel);



    previous.setEnabled(false);
    next.setEnabled(false);
    submit.setEnabled(false);
    cancel.setEnabled(false);
    pagenum.setEnabled(false);
    recordnum.setEnabled(false);






    myBar = new JMenuBar();
    records = new JMenu("Records");
    about= new JMenu("About");
    help= new JMenu("Help");
    view= new JMenu("View");
    sep = new JSeparator();


    newRecord = new JMenuItem("Create New record");
    newRecord.addActionListener(this);
    uer = new JMenuItem("Update Existing Records");
    uer.addActionListener(this);
    deleteRecord = new JMenuItem("Delete Records");
    deleteRecord.addActionListener(this);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);


    byName = new JMenuItem("View By Name");
    byName.addActionListener(this);
    byCompany= new JMenuItem("View By Company");
    byCompany.addActionListener(this);
    byCell = new JMenuItem("View By Cell#");
    byCell.addActionListener(this);






    records.add(newRecord);
    records.add(view);
    records.add(uer );
    records.add(deleteRecord);
    records.add(sep);
    records.add(exit);

    view.add(byName);
    view.add(byCompany);
    view.add(byCell);


    myBar.add(records);
    myBar.add(about);
    myBar.add(help);
    this.setJMenuBar(myBar);




    firstlabel=new JLabel("Name: ");
    tpanel.add(firstlabel);

    firsttext=new JTextField(12);
    tpanel.add(firsttext);
    firstlabel.setLabelFor(firsttext);
    firstlabel.setHorizontalAlignment(SwingConstants.L EADING);



    secondlabel=new JLabel("Company: ");
    tpanel.add(secondlabel);
    secondtext=new JTextField(12);
    tpanel.add(secondtext);
    secondlabel.setLabelFor(secondtext);
    secondlabel.setHorizontalAlignment(SwingConstants. LEADING);


    thirdlabel=new JLabel("Title: ");
    tpanel.add(thirdlabel);
    thirdtext=new JTextField(12);
    tpanel.add(thirdtext);
    thirdlabel.setLabelFor(thirdtext);
    thirdlabel.setHorizontalAlignment(SwingConstants.L EADING);


    fourthlabel=new JLabel("Cell#: ");
    tpanel.add(fourthlabel);
    fourthtext=new JTextField(12);
    tpanel.add(fourthtext);
    fourthlabel.setLabelFor(fourthtext);
    fourthlabel.setHorizontalAlignment(SwingConstants. LEADING);

    fifthlabel=new JLabel("Email: ");
    tpanel.add(fifthlabel);
    fifthtext=new JTextField(14);
    tpanel.add(fifthtext);
    fifthlabel.setLabelFor(fifthtext);
    fifthlabel.setHorizontalAlignment(SwingConstants.L EADING);

    sixthlabel=new JLabel("URL: ");
    tpanel.add(sixthlabel);
    sixthtext=new JTextField(14);
    tpanel.add(sixthtext);
    sixthlabel.setLabelFor(sixthtext);
    sixthlabel.setHorizontalAlignment(SwingConstants.L EADING);



    seventhlabel=new JLabel("Home Phone1: ");
    down2panel.add(seventhlabel);
    seventhtext=new JTextField(12);
    down2panel.add(seventhtext);
    seventhlabel.setLabelFor(seventhtext);
    seventhlabel.setHorizontalAlignment(SwingConstants .LEADING);


    tenthlabel=new JLabel("Work Ph1: ");
    down2panel.add(tenthlabel);
    eighthtext=new JTextField(12);
    down2panel.add(eighthtext);
    tenthlabel.setLabelFor(eighthtext);
    tenthlabel.setHorizontalAlignment(SwingConstants.L EADING);


    eigthenthlabel=new JLabel("Home Phone2: ");
    down2panel.add(eigthenthlabel);

    ninehttext=new JTextField(12);
    down2panel.add(ninehttext);
    eigthenthlabel.setLabelFor(ninehttext);

    eigthenthlabel.setHorizontalAlignment(SwingConstan ts.LEADING);





    eleventhlabel=new JLabel("Work Ph2: ");
    down2panel.add(eleventhlabel);
    tenthtext=new JTextField(12);
    down2panel.add(tenthtext);
    eleventhlabel.setLabelFor(tenthtext);
    eleventhlabel.setHorizontalAlignment(SwingConstant s.LEADING);

    ninethlabel=new JLabel("Home Adress: ");
    down2panel.add(ninethlabel);
    eleventhtext=new JTextField(12);
    down2panel.add(eleventhtext);
    ninethlabel.setLabelFor(eleventhtext);
    ninethlabel.setHorizontalAlignment(SwingConstants. LEADING);

    twelevthlabel=new JLabel("Work Adress: ");
    down2panel.add(twelevthlabel);
    twelevthtext=new JTextField(12);
    down2panel.add(twelevthtext);
    twelevthlabel.setLabelFor(twelevthtext);
    twelevthlabel.setHorizontalAlignment(SwingConstant s.LEADING);



    toppanel.add(rightPanel,BorderLayout.EAST);
    toppanel.add(tpanel,BorderLayout.WEST);


    all2getherpanel.add(toppanel,BorderLayout.NORTH);
    all2getherpanel.add(down2panel,BorderLayout.CENTER );

    outerPanel.add( all2getherpanel,BorderLayout.CENTER);
    outerPanel.add(downpanel,BorderLayout.SOUTH);
    this.add(outerPanel);
    this.setVisible(true);


    }

    public void lightButton(){
    submit.setEnabled(true);
    cancel.setEnabled(true);

    }
    public void darkButton(){
    submit.setEnabled(false);
    cancel.setEnabled(false);

    }
    public void actionPerformed(ActionEvent e) {

    TestJDBC myDB= new TestJDBC();
    GUIpart myGui= new GUIpart();
    Scanner input= new Scanner(System.in);
    myDB.doConnection();



    if(e.getSource()==newRecord){
    myGui.lightButton();

    String name= firsttext.getText();
    String company= secondtext.getText().trim();
    String title= thirdtext.getText().trim();
    String cell= fourthtext.getText().trim();
    String email= fifthtext.getText().trim();
    String url= sixthtext.getText().trim();
    String Home_Phone1= seventhtext.getText().trim();
    String WorkPh1= eighthtext.getText().trim();
    String Home_Phone2= ninehttext.getText().trim();
    String workPh2= tenthtext.getText().trim();
    String home_Address= eleventhtext.getText().trim();
    String work_Address= twelevthtext.getText().trim();


    boolean add= myDB.insertRecod(name, company, title, cell, email,url,Home_Phone1, WorkPh1, Home_Phone2,
    workPh2, home_Address,work_Address);
    if(add)
    JOptionPane.showMessageDialog(null,"Insert Susccessful");
    else
    JOptionPane.showMessageDialog(null,"Whoops!");
    }
    else if(e.getSource()==byName){
    myGui.darkButton();
    pagenum.setEditable(true);
    recordnum.setEditable(true);
    String value = JOptionPane.showInputDialog(this,"Enter The Name: ");

    String [][]name= myDB.getRecords("byName", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    pagenum.setText(String.valueOf(count));
    pagenum.setText(String.valueOf(currentNum));
    recordnum.setText(String.valueOf(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }

    else if(e.getSource()==byCompany){
    myGui.darkButton();
    next.setEnabled(true);
    previous.setEnabled(true);
    String value = JOptionPane.showInputDialog(this,"Enter The Company: ");

    String [][]name= myDB.getRecords("byCompany", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    String count1 = Integer.parseInt(pagenum.setText(count));
    String count2= Integer.parseInt(pagenum.setText(currentNum));
    String count3= Integer.parseInt(recordnum.setText(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }

    else if(e.getSource()==byCell){
    myGui.darkButton();
    next.setEnabled(true);
    previous.setEnabled(true);

    String value = JOptionPane.showInputDialog(this,"Enter The Cell: ");

    String [][]name= myDB.getRecords("byCell", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    String count1 = Integer.parseInt(pagenum.setText(count));
    String count2= Integer.parseInt(pagenum.setText(currentNum));
    String count3= Integer.parseInt(recordnum.setText(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }
    else if(e.getSource()==uer){
    String ID= JOptionPane.showInputDialog(this,"Enter The ID to Update: ");

    int IDs= Integer.parseInt(ID);

    if(ID == "addressID"){

    String name= firsttext.getText();
    String company= secondtext.getText();
    String title= thirdtext.getText();
    String cell= fourthtext.getText();
    String email= fifthtext.getText();
    String url= sixthtext.getText();
    String Home_Phone1= seventhtext.getText();
    String WorkPh1= eighthtext.getText();
    String Home_Phone2= ninehttext.getText();
    String workPh2= tenthtext.getText();
    String home_Address= eleventhtext.getText();
    String work_Address= twelevthtext.getText();

    myGui.lightButton();
    myDB.update(Integer.parseInt(ID), name, company, title, cell, email, url,Home_Phone1, WorkPh1,Home_Phone2,workPh2,home_Address,work_Addr ess);
    JOptionPane.showMessageDialog(null, "Update Sucessfull");
    }
    else{
    JOptionPane.showMessageDialog(null, "The ID Is not Available");

    }
    }

    }
    /*else if(e.getSource()==deleteRecord){
    String ID= JOptionPane.showInputDialog(this,"Enter The ID to Update: ");

    int IDs= Integer.parseInt(ID);

    if(ID == "addressID"){

    myGui.darkButton();
    next.setEnabled(false);
    previous.setEnabled(false);
    JOptionPane.showInputDialog(this,"Enter The ID to Delete: ");
    myDB.deleting(IDs);
    }
    else{
    JOptionPane.showMessageDialog(null, "The ID Is not Available");}


    }
    myDB.closeDB();*/


    }


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



    }






    jdbc part:::



    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import java.awt.Dimension;
    import java.sql.SQLException;
    import java.util.Scanner;

    public class GUIpart extends JFrame implements ActionListener{


    String[] fields = {"Name: ", "Company: ", "Title: ", "Mobile: ", "Email: ", "URL: ", "Phone home 1: ", "Phone work 1: ", "Phone home 2: ", "Phone work 2: ", "Home address: ", "Work address: "};
    String[][] results;
    JButton submit, cancel, next, previous;

    JPanel outerPanel;

    JPanel tpanel;
    JPanel downpanel;
    JPanel toppanel;
    JPanel rightPanel;
    JPanel down2panel ;
    JPanel all2getherpanel;




    JMenu records;
    JMenu about;
    JMenu help;
    JMenu view;
    JMenuBar myBar;
    JSeparator sep;
    JMenuItem newRecord, uer, deleteRecord, exit, byName, byCompany, byCell;


    JLabel firstlabel,secondlabel,thirdlabel,fourthlabel,fift hlabel,sixthlabel,seventhlabel,eigthenthlabel,nine thlabel,tenthlabel,eleventhlabel,twelevthlabel;


    JTextField pagenum, recordnum, firsttext,secondtext,thirdtext,fourthtext,fifthtex t,sixthtext,seventhtext,eighthtext,ninehttext,tent htext,eleventhtext,twelevthtext;

    GUIpart(){

    setTitle("Address Book-ITE 409-Project");
    setSize(950,400);
    setLocation(210,110);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createGUI();

    }
    public void createGUI(){


    outerPanel= new JPanel();
    tpanel=new JPanel();
    toppanel= new JPanel();
    rightPanel= new JPanel();
    down2panel=new JPanel();
    all2getherpanel=new JPanel();



    all2getherpanel.setLayout(new BorderLayout());
    outerPanel.setLayout(new BorderLayout());


    tpanel.setLayout(new GridLayout(6,2,8,8));


    toppanel.setLayout(new BorderLayout());

    rightPanel.setLayout(null);
    down2panel.setLayout(new GridLayout(3,2));





    downpanel =new JPanel();
    previous=new JButton("Previous");
    previous.addActionListener(this);

    next=new JButton("Next");
    next.addActionListener(this);

    submit=new JButton("Submit");
    submit.addActionListener(this);

    cancel=new JButton("Cancel");
    cancel.addActionListener(this);

    downpanel.setLayout(new FlowLayout());

    pagenum= new JTextField(12);
    pagenum.addActionListener(this);

    recordnum=new JTextField(12);
    recordnum.addActionListener(this);

    downpanel.add( pagenum);
    downpanel.add(recordnum);



    downpanel.add(previous);
    downpanel.add(next);
    downpanel.add(submit);
    downpanel.add(cancel);



    previous.setEnabled(false);
    next.setEnabled(false);
    submit.setEnabled(false);
    cancel.setEnabled(false);
    pagenum.setEnabled(false);
    recordnum.setEnabled(false);






    myBar = new JMenuBar();
    records = new JMenu("Records");
    about= new JMenu("About");
    help= new JMenu("Help");
    view= new JMenu("View");
    sep = new JSeparator();


    newRecord = new JMenuItem("Create New record");
    newRecord.addActionListener(this);
    uer = new JMenuItem("Update Existing Records");
    uer.addActionListener(this);
    deleteRecord = new JMenuItem("Delete Records");
    deleteRecord.addActionListener(this);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);


    byName = new JMenuItem("View By Name");
    byName.addActionListener(this);
    byCompany= new JMenuItem("View By Company");
    byCompany.addActionListener(this);
    byCell = new JMenuItem("View By Cell#");
    byCell.addActionListener(this);






    records.add(newRecord);
    records.add(view);
    records.add(uer );
    records.add(deleteRecord);
    records.add(sep);
    records.add(exit);

    view.add(byName);
    view.add(byCompany);
    view.add(byCell);


    myBar.add(records);
    myBar.add(about);
    myBar.add(help);
    this.setJMenuBar(myBar);




    firstlabel=new JLabel("Name: ");
    tpanel.add(firstlabel);

    firsttext=new JTextField(12);
    tpanel.add(firsttext);
    firstlabel.setLabelFor(firsttext);
    firstlabel.setHorizontalAlignment(SwingConstants.L EADING);



    secondlabel=new JLabel("Company: ");
    tpanel.add(secondlabel);
    secondtext=new JTextField(12);
    tpanel.add(secondtext);
    secondlabel.setLabelFor(secondtext);
    secondlabel.setHorizontalAlignment(SwingConstants. LEADING);


    thirdlabel=new JLabel("Title: ");
    tpanel.add(thirdlabel);
    thirdtext=new JTextField(12);
    tpanel.add(thirdtext);
    thirdlabel.setLabelFor(thirdtext);
    thirdlabel.setHorizontalAlignment(SwingConstants.L EADING);


    fourthlabel=new JLabel("Cell#: ");
    tpanel.add(fourthlabel);
    fourthtext=new JTextField(12);
    tpanel.add(fourthtext);
    fourthlabel.setLabelFor(fourthtext);
    fourthlabel.setHorizontalAlignment(SwingConstants. LEADING);

    fifthlabel=new JLabel("Email: ");
    tpanel.add(fifthlabel);
    fifthtext=new JTextField(14);
    tpanel.add(fifthtext);
    fifthlabel.setLabelFor(fifthtext);
    fifthlabel.setHorizontalAlignment(SwingConstants.L EADING);

    sixthlabel=new JLabel("URL: ");
    tpanel.add(sixthlabel);
    sixthtext=new JTextField(14);
    tpanel.add(sixthtext);
    sixthlabel.setLabelFor(sixthtext);
    sixthlabel.setHorizontalAlignment(SwingConstants.L EADING);



    seventhlabel=new JLabel("Home Phone1: ");
    down2panel.add(seventhlabel);
    seventhtext=new JTextField(12);
    down2panel.add(seventhtext);
    seventhlabel.setLabelFor(seventhtext);
    seventhlabel.setHorizontalAlignment(SwingConstants .LEADING);


    tenthlabel=new JLabel("Work Ph1: ");
    down2panel.add(tenthlabel);
    eighthtext=new JTextField(12);
    down2panel.add(eighthtext);
    tenthlabel.setLabelFor(eighthtext);
    tenthlabel.setHorizontalAlignment(SwingConstants.L EADING);


    eigthenthlabel=new JLabel("Home Phone2: ");
    down2panel.add(eigthenthlabel);

    ninehttext=new JTextField(12);
    down2panel.add(ninehttext);
    eigthenthlabel.setLabelFor(ninehttext);

    eigthenthlabel.setHorizontalAlignment(SwingConstan ts.LEADING);





    eleventhlabel=new JLabel("Work Ph2: ");
    down2panel.add(eleventhlabel);
    tenthtext=new JTextField(12);
    down2panel.add(tenthtext);
    eleventhlabel.setLabelFor(tenthtext);
    eleventhlabel.setHorizontalAlignment(SwingConstant s.LEADING);

    ninethlabel=new JLabel("Home Adress: ");
    down2panel.add(ninethlabel);
    eleventhtext=new JTextField(12);
    down2panel.add(eleventhtext);
    ninethlabel.setLabelFor(eleventhtext);
    ninethlabel.setHorizontalAlignment(SwingConstants. LEADING);

    twelevthlabel=new JLabel("Work Adress: ");
    down2panel.add(twelevthlabel);
    twelevthtext=new JTextField(12);
    down2panel.add(twelevthtext);
    twelevthlabel.setLabelFor(twelevthtext);
    twelevthlabel.setHorizontalAlignment(SwingConstant s.LEADING);



    toppanel.add(rightPanel,BorderLayout.EAST);
    toppanel.add(tpanel,BorderLayout.WEST);


    all2getherpanel.add(toppanel,BorderLayout.NORTH);
    all2getherpanel.add(down2panel,BorderLayout.CENTER );

    outerPanel.add( all2getherpanel,BorderLayout.CENTER);
    outerPanel.add(downpanel,BorderLayout.SOUTH);
    this.add(outerPanel);
    this.setVisible(true);


    }

    public void lightButton(){
    submit.setEnabled(true);
    cancel.setEnabled(true);

    }
    public void darkButton(){
    submit.setEnabled(false);
    cancel.setEnabled(false);

    }
    public void actionPerformed(ActionEvent e) {

    TestJDBC myDB= new TestJDBC();
    GUIpart myGui= new GUIpart();
    Scanner input= new Scanner(System.in);
    myDB.doConnection();



    if(e.getSource()==newRecord){
    myGui.lightButton();

    String name= firsttext.getText();
    String company= secondtext.getText().trim();
    String title= thirdtext.getText().trim();
    String cell= fourthtext.getText().trim();
    String email= fifthtext.getText().trim();
    String url= sixthtext.getText().trim();
    String Home_Phone1= seventhtext.getText().trim();
    String WorkPh1= eighthtext.getText().trim();
    String Home_Phone2= ninehttext.getText().trim();
    String workPh2= tenthtext.getText().trim();
    String home_Address= eleventhtext.getText().trim();
    String work_Address= twelevthtext.getText().trim();


    boolean add= myDB.insertRecod(name, company, title, cell, email,url,Home_Phone1, WorkPh1, Home_Phone2,
    workPh2, home_Address,work_Address);
    if(add)
    JOptionPane.showMessageDialog(null,"Insert Susccessful");
    else
    JOptionPane.showMessageDialog(null,"Whoops!");
    }
    else if(e.getSource()==byName){
    myGui.darkButton();
    pagenum.setEditable(true);
    recordnum.setEditable(true);
    String value = JOptionPane.showInputDialog(this,"Enter The Name: ");

    String [][]name= myDB.getRecords("byName", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    pagenum.setText(String.valueOf(count));
    pagenum.setText(String.valueOf(currentNum));
    recordnum.setText(String.valueOf(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }

    else if(e.getSource()==byCompany){
    myGui.darkButton();
    next.setEnabled(true);
    previous.setEnabled(true);
    String value = JOptionPane.showInputDialog(this,"Enter The Company: ");

    String [][]name= myDB.getRecords("byCompany", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    String count1 = Integer.parseInt(pagenum.setText(count));
    String count2= Integer.parseInt(pagenum.setText(currentNum));
    String count3= Integer.parseInt(recordnum.setText(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }

    else if(e.getSource()==byCell){
    myGui.darkButton();
    next.setEnabled(true);
    previous.setEnabled(true);

    String value = JOptionPane.showInputDialog(this,"Enter The Cell: ");

    String [][]name= myDB.getRecords("byCell", value);
    int count=myDB.getResultCount();
    int currentNum= 1;

    try{
    while(currentNum <= count){
    for(int i=0; i<count; i++) {
    firsttext.setText(name[i][0]);
    secondtext.setText(name[i][1]);
    thirdtext.setText(name[i][2]);
    fourthtext.setText(name[i][3]);
    sixthtext.setText(name[i][4]);
    eighthtext.setText(name[i][5]);
    ninehttext.setText(name[i][6]);
    tenthtext.setText(name[i][7]);
    eleventhtext.setText(name[i][8]);
    twelevthtext.setText(name[i][9]);

    String count1 = Integer.parseInt(pagenum.setText(count));
    String count2= Integer.parseInt(pagenum.setText(currentNum));
    String count3= Integer.parseInt(recordnum.setText(count));

    if(e.getSource()==next){
    currentNum++;

    }
    else if(e.getSource()==previous){
    currentNum--;
    }

    }
    }
    }

    catch (SQLException ae) {
    System.out.println("Error: Could not complete query");
    System.exit(2);
    }


    }
    else if(e.getSource()==uer){
    String ID= JOptionPane.showInputDialog(this,"Enter The ID to Update: ");

    int IDs= Integer.parseInt(ID);

    if(ID == "addressID"){

    String name= firsttext.getText();
    String company= secondtext.getText();
    String title= thirdtext.getText();
    String cell= fourthtext.getText();
    String email= fifthtext.getText();
    String url= sixthtext.getText();
    String Home_Phone1= seventhtext.getText();
    String WorkPh1= eighthtext.getText();
    String Home_Phone2= ninehttext.getText();
    String workPh2= tenthtext.getText();
    String home_Address= eleventhtext.getText();
    String work_Address= twelevthtext.getText();

    myGui.lightButton();
    myDB.update(Integer.parseInt(ID), name, company, title, cell, email, url,Home_Phone1, WorkPh1,Home_Phone2,workPh2,home_Address,work_Addr ess);
    JOptionPane.showMessageDialog(null, "Update Sucessfull");
    }
    else{
    JOptionPane.showMessageDialog(null, "The ID Is not Available");

    }
    }

    }
    /*else if(e.getSource()==deleteRecord){
    String ID= JOptionPane.showInputDialog(this,"Enter The ID to Update: ");

    int IDs= Integer.parseInt(ID);

    if(ID == "addressID"){

    myGui.darkButton();
    next.setEnabled(false);
    previous.setEnabled(false);
    JOptionPane.showInputDialog(this,"Enter The ID to Delete: ");
    myDB.deleting(IDs);
    }
    else{
    JOptionPane.showMessageDialog(null, "The ID Is not Available");}


    }
    myDB.closeDB();*/


    }

    import java.sql.*;
    import java.util.Scanner;


    public class TestJDBC {

    String driverName = "com.mysql.jdbc.Driver"; // Connect to MySQL's JDBC driver
    String serverName= "localhost";
    String port = "3306";
    String databaseName="addressbook";
    String url = "jdbc:mysql://"+serverName+":"+port+"/"+databaseName;

    String username = "root";
    String password = "dida123";
    int resultCount;

    Connection connection;

    public TestJDBC(){}

    public boolean doConnection(){

    try{

    Class.forName(driverName);

    connection = DriverManager.getConnection(url, username, password);

    }catch(ClassNotFoundException e){
    System.out.println("Driver/ Class not found!\n"+e.getMessage());
    return false;
    }
    catch(SQLException e){
    System.out.println("SQL Exception: "+e.getMessage());
    return false;
    }
    return true;
    }
    public boolean insertRecod(String name, String company, String title, String cellPhone,
    String email, String url, String home_Phone1, String home_Phone2, String work_Phone1,
    String work_Phone2, String home_address, String work_address){
    Statement stmt= null;


    String query= "insert into adressbook values (null,'" +name+"','"+company+"',"

    +title+ "','"+cellPhone+"','"+email+"','"+url+"','"+home_P hone1+"','"+home_Phone2+
    "','"+work_Phone1+"','"+work_Phone2+"','"+home_add ress+"','"+work_address+");";
    try{


    stmt=connection.createStatement();
    stmt.executeUpdate(query);
    System.out.print("Insert Has been Done Successfully");
    }

    catch(SQLException e){
    System.out.print(e.getMessage());}

    finally{
    try{
    stmt.close();
    }catch(SQLException e){
    System.out.print(e.getMessage());
    return false;
    }
    }
    return true;
    }

    public s update(int id, String name, String company, String title,String cellphone, String email, String url, String home_phone1,String home_phone2,
    String work_phone1, String work_phone2,String home_address, String work_address){
    Statement stmt2= null;

    String query1 = "update address set name ='"+name+" where addressID = id;";
    String query2 = "update address set company ='"+company+" where addressID = id;";
    String query3 = "update address set title ='"+title+" where addressID = id;";
    String query4 = "update address set cellPhone ='"+cellphone+" where addressID = id;";
    String query5 = "update address set email ='"+email+" where addressID = id;";
    String query7 = "update address set url ='"+url+" where addressID = id;";
    String query8 = "update address set home_Phone1 ='"+home_phone1+" where addressID = id;";
    String query9 = "update address set home_Phone2 ='"+home_phone2+" where addressID = id;";
    String query10 = "update address set work_Phone1 ='"+work_phone1+" where addressID = id;";
    String query11 = "update address set work_Phone2 ='"+work_phone2+" where addressID = id;";
    String query12 = "update address set home_address ='"+home_address+" where addressID = id;";
    String query13 = "update address set work_address ='"+work_address+" where addressID = id;";

    try{
    stmt2=connection.createStatement();
    stmt2.executeUpdate(query1);
    stmt2.executeUpdate(query2);
    stmt2.executeUpdate(query3);
    stmt2.executeUpdate(query4);
    stmt2.executeUpdate(query5);
    stmt2.executeUpdate(query7);
    stmt2.executeUpdate(query8);
    stmt2.executeUpdate(query9);
    stmt2.executeUpdate(query10);
    stmt2.executeUpdate(query11);
    stmt2.executeUpdate(query12);
    stmt2.executeUpdate(query13);

    System.out.print("The Update Has been Done Successfully ");
    }catch(SQLException e){
    System.out.println(e.getMessage());


    }
    finally{
    try{
    stmt2.close();
    }catch(SQLException e){
    System.out.print(e.getMessage());

    }
    }


    }

    public boolean deleting(int Id){
    Statement stmt3=null;

    String query="delete from adressbook where addressID="+Id+";";

    try{
    stmt3=connection.createStatement();

    stmt3.executeUpdate(query);
    }catch (SQLException e){
    System.out.println(e.getMessage());

    return false;


    }
    finally{
    if(stmt3 != null){
    System.out.print("It has Been Deleted ! ");

    }

    }
    return true;


    }




    public String[][] getRecords(String field, String value) throws SQLException {
    String[][] results = new String[50][13];
    String query;
    PreparedStatement pstmt = null;
    if(field == "name") {
    query = "SELECT * FROM contacts WHERE name=?";
    }
    else if(field == "company") {
    query = "SELECT * FROM contacts WHERE company=?";
    }
    else {
    query = "SELECT * FROM contacts WHERE mobile=?";
    }
    pstmt = connection.prepareStatement(query);
    pstmt.setString(1,value);
    ResultSet rs = pstmt.executeQuery();
    resultCount = 0;
    if (!rs.next()) {
    resultCount = 0;}
    else{


    do {


    results[resultCount][0] = rs.getString("addressID");
    results[resultCount][1] = rs.getString("name");
    results[resultCount][2] = rs.getString("company");
    results[resultCount][3] = rs.getString("title");
    results[resultCount][4] = rs.getString("cellPhone");
    results[resultCount][5] = rs.getString("email");
    results[resultCount][6] = rs.getString("url");
    results[resultCount][7] = rs.getString("home_phone1");
    results[resultCount][8] = rs.getString("home_phone2");
    results[resultCount][9] = rs.getString("work_phone1");
    results[resultCount][10] = rs.getString("work_phone2");
    results[resultCount][11] = rs.getString("home_address");
    results[resultCount][12] = rs.getString("work_address");

    resultCount++;


    } while(rs.next());

    }
    pstmt.close();
    return results;
    }


    public int getResultCount() {
    return resultCount;
    }




    public void closeDB(){
    try{
    connection.close();
    }catch(SQLException e){
    System.out.println(e.getMessage());
    }
    }


    }






    this is what I get when I run the program



    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    The method update(int, String, String, String, String, String, String, String, String, String, String, String, String) from the type TestJDBC refers to the missing type s

    at GUIpart.actionPerformed(GUIpart.java:418)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unk nown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    --- Update ---

    the gui is working just fine and i dont think that the jdbc part has a problem either , my problem is in the actionPerformed idont know how to retreive the result set and display each record in a textfield in my gui?? can anyone help me ? I appreciate it


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: jdbc integration with Gui to show user information retrieved from mysql

    Welcome harem_najat

    Please see the Announcements page for the use of code tags and other useful forum tips.
    The method setText(String) in the type JTextComponent is not applicable for the arguments (int)
    int is not the same thing as a String. The error tells you (approximately) where to look and what to look for. Change the int to a String (in one way or another) or figure out why there is an int being assigned instead of the intended String.

    Generally posting lengthy code is frowned upon, but in finding an error it is best to post the unmodified version, in either case no one wants to read a bunch of black and white, left-justified syntax.

Similar Threads

  1. jdbc jsp servlet integration
    By akram in forum JavaServer Pages: JSP & JSTL
    Replies: 6
    Last Post: May 7th, 2012, 06:49 AM
  2. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 11:54 PM
  3. JDBC+MYSQL
    By maya700 in forum JDBC & Databases
    Replies: 1
    Last Post: September 19th, 2011, 02:30 PM
  4. JNDI - JDBC Integration problem
    By rangarajank in forum JDBC & Databases
    Replies: 0
    Last Post: September 29th, 2009, 06:07 AM