2 Replies - 976 Views - Last Post: 21 July 2013 - 06:31 PM Rate Topic: -----

#1 Koshka   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 13

Election problem with database connection

Posted 21 July 2013 - 10:32 AM

I need to establish a connection with a database created in access to a java class in NetBeans IDE. There needs to be a GUI with a means to vote for various people or parties and count these votes at the end and display the results in a graph and table. There need to be options to add a new field or edit or delete an existing one using sql statements. This is what I have at the moment. I need to find a way to add a home screen (JFrameForm?) from where you can go on to the login screen then either the voting screen or admin screen where you can edit candidates by using the sql statements.

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

public class Login 
{
    Connection con;
    Statement st;
    ResultSet rs;
    
    JFrame f = new JFrame("User Login");
    JLabel userlabel = new JLabel ("Username: ");
    JLabel passlabel = new JLabel ("Password: ");
    JTextField usertext = new JTextField(15);
    JTextField passtext = new JTextField(10);
    JButton btn1 = new JButton("Login");
    JButton btn2 = new JButton ("Register");
    
    JFrame f1 = new JFrame("Home Page");
    JPanel p1 = new JPanel();
    JLabel welcome = new JLabel ("WELCOME!");
    JButton btn3 = new JButton ("Enter");
    
    public Login()
    {
        connect();
        frame();
    }
    
    public void connect()
    {
        try
        {
        
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);
        
        String db = "jdbc:odbc:Votingdb";
        con = DriverManager.getConnection(db);
        st = con.createStatement();
        
        }
        catch (Exception ex)
        {
            
        }
    }
    
    public void frame()
    {
        f.setSize(600,100);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        
        JPanel p = new JPanel();
        p.add(userlabel);
        p.add(usertext);
        p.add(passlabel);
        p.add(passtext);
        p.add(btn1);
        p.add(btn2);
        
        f.add(p);
        
        f1.setSize(600,600);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        p1.add(welcome);
        p1.add(btn3);
        f1.add(p1);
        
        btn1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                String user = usertext.getText().trim();
                String pass = passtext.getText().trim();
                
                String sql = "SELECT UsersTbl.Name, UsersTbl.Password FROM UsersTbl WHERE (((UsersTbl.Name)= '"+user+"') AND ((UsersTbl.Password)= '"+pass+"'))";
                rs = st.executeQuery(sql);
                
                int count = 0;
                
                while (rs.next())
                {
                    count = count + 1;
                }
                
                if (count == 1)
                {
                    JOptionPane.showMessageDialog(null, "User Found, Access Granted");
                    f.setVisible(false);
                    f1.setVisible(true);
                }
                else if (count > 1)
                {
                   JOptionPane.showMessageDialog(null, "Duplicate user, Access Denied"); 
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "User not found!");
                }
                
                }
                catch (Exception ex)
                {
                    
                }
            }
        });
        
        btn2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                String userReg = usertext.getText().trim();
                String passReg = passtext.getText().trim();
                
                String sql = "INSERT INTO UsersTbl (Name, Password) VALUES ('"+userReg+"' , '"+passReg+"')";
                String sql1 = "SELECT UsersTbl.Name, UsersTbl.Password FROM UsersTbl";
                rs = st.executeQuery(sql);
                rs = st.executeQuery(sql1);
                }
                catch (Exception ex)
                {
                    
                }
        }
        
    });
    }
    public static void main (String[] args)
    {
        new Login();
    }
}


This post has been edited by macosxnerd101: 21 July 2013 - 10:34 AM
Reason for edit:: Code goes between the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Election problem with database connection

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Election problem with database connection

Posted 21 July 2013 - 01:53 PM

You have lots of "needs" but you haven't asked a specific question or described the problem you are having.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Election problem with database connection

Posted 21 July 2013 - 06:31 PM

View PostKoshka, on 21 July 2013 - 01:32 PM, said:

I need to establish a connection with a database created in access to a java class in NetBeans IDE. There needs to be a GUI with a means to vote for various people or parties and count these votes at the end and display the results in a graph and table.

So what is the question ?
What is the use of NetBeans as Editor changes something in your question ? Does the code written with NetBeans does not behave as the code written with another IDE ? or NotePad ? or UltraEdit ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1