• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

having trouble display the name from my database

April 11, 2020 by

Questions › having trouble display the name from my database
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I want to print the user name after logging in with his ID in another JFrame

I followed this method but it does not work

//this class to login
 public mClass() {
// Frame build :
        setSize(800, 600);
        setTitle(" page1  ");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width / 2 - getWidth() / 2,
                size.height / 3 - getHeight() / 3);
        //Panel & BackGround :
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setBackground(Color.decode("#DFDDCE"));

        // text box
        JTextField user = new JTextField();
        user.setBounds(245, 300, 300, 40);
        user.setFont(newFonts);
        user.setForeground(Color.decode("#5195E1"));
        panel.add(user);
        // text for box :
        JLabel tx = new JLabel(" Enter your ID to login.");
        tx.setBounds(245, 30, size.width, size.height);
        panel.add(tx);
        //submit button :
        JButton login = new JButton(" login ");
        login.setBounds(245, 355, 300, 40);
        login.setBorder(new LineBorder(Color.decode("#5195E1")));
           login.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            // Class.forName("com.mysql.jdbc.Driver");
                            String host = "jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC";
                            String uName = "root"; String uPass = "dataroot";
                            Connection con = DriverManager.getConnection(host, uName, uPass);
                            String password=user.getText();
                            String sql = "SELECT * FROM mstable where m_pass=? ";
                            PreparedStatement pst = con.prepareStatement(sql);
                            pst.setString(1,password);
                            ResultSet rs = pst.executeQuery();
                            if(rs.next()) {
                                new welcomeMs();
                                dispose();
                            }
                            else { JOptionPane.showMessageDialog(null,"wrong id , Please try again ");
                                 } }
                        catch ( SQLException ex) { System.out.println(ex); }
                       } });
           panel.add(login);
        //back button :
        setVisible(true);

    }
    public String getuserName() {
        return this.user;
    }
}


i want to show the name in this class

// i extend this class to the last Class
try {
            // Class.forName("com.mysql.jdbc.Driver");
            String host = "jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC";
            String uName = "root";
            String uPass = "dataroot";
            Connection con = DriverManager.getConnection(host, uName, uPass);
            String sql = "SELECT m_name FROM mstable where m_pass = 'getuserName()' ";
            PreparedStatement pst = con.prepareStatement(sql);
            //pst.setString(1, getuserName());
            ResultSet rs = pst.executeQuery();
            rs.next();
                JLabel label = new JLabel();
                label.setText(rs.getString(1) + " welcome ");
                label.setBounds(402, -295, size.width, size.height);
                panel.add(label);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,ex);
        }
f.setVisible(true);

When I try to log in, this message appears " lllegal operation on empty result set " or when i remove the // " Parameter index is out of range (1> number of parameters, which is 0)."

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: javascript, jframe, sql, sql-server, swingworker

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023