Java

JDBC Example3

JDBC Example to show records on JTable

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

public class JTableDatabase 
{
public static void main(String[] args) 
{
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test");
String sql = "Select * from product";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception e){
System.out.println(e);
}
JTable table = new JTable(data, columnNames);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane( table );
p.add( scrollPane );
JFrame f=new JFrame();
f.add(p);
f.setSize(600,400);
f.setVisible(true);
}
}

Out of the above program is as following image…

Java Program to demonstrate Linked List

import java.util.*;
public class LinkedListExa 
{
public static void main(String args[]) 
{

LinkedList<String> linkedlist = new LinkedList<String>();

         linkedlist.add("Beta");
         linkedlist.add("Cupcake");
         linkedlist.add("Donut");
         linkedlist.add("Eclair");
         linkedlist.add("Froyo");
         linkedlist.add("Gingerbread");
         linkedlist.add("Honeycomb");
         linkedlist.add("Ice Cream Sandwich");
         linkedlist.add("Jellybean");
         linkedlist.add("KitKat");

         System.out.println("Linked List Content: " +linkedlist);

         /*Adding  First and Last Element */

         linkedlist.addFirst("Alpha");
         linkedlist.addLast("Lollipop");

         System.out.println("LinkedList Content after addition: " +linkedlist);

         Object firstvar = linkedlist.get(0);
         System.out.println("First element: " +firstvar);

         linkedlist.set(0, "Changed first item");
         Object firstvar2 = linkedlist.get(0);
         System.out.println("First element after update by set method: " +firstvar2);

         /*Remove first and last element*/
         linkedlist.removeFirst();
         linkedlist.removeLast();
         System.out.println("LinkedList after deletion of first and last element: " +linkedlist);

         /* Add to a Position and remove from a position*/

         linkedlist.add(0, "Mango");
         linkedlist.remove(2);
         System.out.println("Final Content: " +linkedlist); 
     }
}

JDBC program to demonstrate Login program.

//package awt;

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 l = new JLabel("Username : ");
        JLabel l1= new JLabel("Password : ");
        JTextField t = new JTextField (10);
        JTextField t1 = new JTextField (10);
        JButton b = new JButton("Login");

        public Login()
    {
            connect();
            frame();

     }
        
        public void connect()
    {
               try {
            
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);

            String db = "jdbc:odbc:dsntest";
            con = DriverManager.getConnection(db);
            st = con.createStatement();


        }catch (Exception e )
               {
        }
    }
        
        public void frame()
    {
        f.setSize(600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        JPanel p = new JPanel();
        p.add(l);
        p.add(t);
        p.add(l1);
        p.add(t1);
        p.add(b);

        f.add(p);
        
        b.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                try
                {

            String user =t.getText().trim();
            String pass = t1.getText().trim();

 String sql = "select usernm, pass from login_tab where 

usernm = '" + user +"'and pass= '"+ pass +"'";
            rs = st.executeQuery(sql);


            int count = 0;
            while(rs.next()){

                count = count + 1;
            }

            if (count == 1 )
            {
             JOptionPane.showMessageDialog(null,"User Found , 

Access Granted! ");
             }
            else if ( count > 1 )
             {
            JOptionPane.showMessageDialog(null,"Duplicate User 

, Access Denied! ");
             }
            else
            {
            JOptionPane.showMessageDialog(null," User Does Not 

Exist ! ");

            }

                } catch(Exception ex ){

                }

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

            new Login();

    }
}

Output is…

Java Swing program (JDBC) Login to MySQL Database.

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

class LoginDemo extends JFrame{
 JButton SUBMIT;
 JLabel label1,label2;
 final JTextField  text1,text2;

LoginDemo()
{
    setTitle("Login Form");
    setLayout(null);
    label1 = new JLabel();
    label1.setText("Username:");
    text1 = new JTextField(15);

    label2 = new JLabel();
    label2.setText("Password:");
    text2 = new JPasswordField(15);

    SUBMIT=new JButton("SUBMIT");
    label1.setBounds(350,100,100,20);
    text1.setBounds(450,100,200,20);
    label2.setBounds(350,130,100,20);
    text2.setBounds(450,130,200,20);
    SUBMIT.setBounds(450,160,100,20);
   add(label1);
   add(text1);
   add(label2);
   add(text2);
   add(SUBMIT);

   setVisible(true);
   setSize(1024,768);

 SUBMIT.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent ae){
    String value1=text1.getText();
    String value2=text2.getText();
    try{
 Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:90/test", "root", "");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'");
           String uname="",pass="";
           if(rs.next()){
               uname=rs.getString("username");
               pass=rs.getString("password");
           }
 if(value1.equals("") && value2.equals("")) {
      JOptionPane.showMessageDialog(null,"Enter login name or password","Error",JOptionPane.ERROR_MESSAGE);
  }
 else if(value1.equals(uname) && value2.equals(pass)) {
    NextPage page=new NextPage(uname);
    page.setVisible(true);
    }
 else if (!value1.equals(uname) && !value2.equals(pass)) {
     text1.setText("");
     text2.setText("");
    }
    }
    catch(Exception e){}
}
 });
  }

  public static void main(String arg[]){
  new LoginDemo();
}
}
SKB

Leave a Reply

Your email address will not be published. Required fields are marked *