Sample Project Template java

1) Login form

package xyz;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JProgressBar;

import java.sql.*;

class Login extends JFrame implements ActionListener
{ JPasswordField p1;
JTextField t1,t2;
Button b1;
JFrame frame;
JLabel l1,l2;
Login()
{
l1=new JLabel(“USER ID”);
l1.setBounds(10, 100, 100,30 );

add(l1);

t1=new JTextField();
t1.setBounds(150, 100, 100,30 );

add(t1);

l2=new JLabel(“PASSWORD”);
l2.setBounds(10, 200, 100,30 );

add(l2);

p1=new JPasswordField();
p1.setBounds(150, 200, 100,30 );
add(p1);

b1=new Button(“Login”);

b1.setBounds(100, 250, 100,30 );
add(b1);
b1.addActionListener(this);

//frame.setUndecorated(true);
// frame.setResizable(false);
setBackground(Color.GRAY);
setSize(500,500);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try
{
if (arg0.getSource()==b1){
String uid=t1.getText();
String pwd=p1.getText();
if(uid.equals(“Apurva”)&& pwd.equals(“Test123″))
{
JOptionPane.showMessageDialog(frame,”LOGIN SUCCESSFULL”);
this.dispose();
DBMS b1=new DBMS();
}
else
{
JOptionPane.showMessageDialog(frame,”LOGIN FAILED”);
//this.dispose();
}

}
}
catch(Exception e)
{
System.out.println(e);
}
}

public static void main(String args[])
{
@SuppressWarnings(“unused”)
Login g1=new Login();

}

}

2) Class For registration
note

    Using Database Bill
    Using table with two fields
    setting value of T1 Textfield to both the fields
    Clicking on next button will open next screen
    Clicking on Save will save the value to database

package xyz;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JProgressBar;

import java.sql.*;

class DBMS extends JFrame implements ActionListener
{
JTextField t2;
Button b1,b2;
DBMS()
{

JTextField t1;

JTextField t3;
JTextField t5;
JTextField t6;
JTextField t7;
JComboBox combo;
JFrame frame;

t2=new JTextField();
t2.setBounds(100, 200, 100,30 );

add(t2);

b1=new Button(“Next”);

b1.setBounds(100, 250, 100,30 );
add(b1);
b1.addActionListener(this);
b2=new Button(“Save”);
b2.setBounds(240, 250, 100,30 );
add(b2);
b2.addActionListener(this);
//frame.setUndecorated(true);
// frame.setResizable(false);
setBackground(Color.GRAY);
setSize(500,500);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try
{
if (arg0.getSource()==b1){
Second s1=new Second();
s1.setVisible(true);
this.dispose();
}
if (arg0.getSource()==b2){
String x=t2.getText();
String y=t2.getText();

Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/Bill”,”root”,”Test@123″);
Statement stmt=con.createStatement();

String sql= “INSERT INTO Bill1 VALUES (“+x+”, “+y+”)”;
//while(rs.next())
//System.out.println(rs.getInt(1)+” “+rs.getString(2)+” “+rs.getString(3));
stmt.executeUpdate(sql);
con.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}

public static void main(String args[])
{
//@SuppressWarnings(“unused”)
// DBMS g1=new DBMS();

}

}

3) Switching to next screen
//write your code in this class for second form or so on

package xyz;

import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Second extends JFrame {

Second()
{ setLayout(null);
setSize(500,600);
JLabel l1=new JLabel(“Welcome to second Frame”);
l1.setBounds(100, 200, 100, 30);
add(l1);

}

}

Advertisements

Connect Java Program to mysql database

step 1: create mysql Database and tables

create database studinfo;

use studinfo;

create table stud(id int(10), name varchar(20), dept varchar(20), phn int(12));

step 2: create java program

javaconnectivity
Step 3: To Execute this program you require mysql connector

Download from here….
mysql Connector

Step 4: Configure Eclipse

1.1 Start Eclipse ->  File -> New Project -> Give any name to project -> finish

CreateProject

1.2 right click on your project in package explorer ->new-> class-> give class name and optional package name

newclass

Note: your Class name should always start with “Uppercase letter”.

1.3 copy following code and and Paste in your java class created above

package studinfo;

import java.sql.*;
class StudInfoDatabaseConnection
{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection
( “jdbc:mysql://localhost:3306/studinfo”,”root”,”root”);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(“select * from stud”);
while(rs.next())
System.out.println (rs.getInt(1)+” “+rs.getString(2)+” “+rs.getString(3)+ ” ” + rs.getInt(4));
con.close();
}
catch(Exception e){ System.out.println(e);
}
}

1.4 right click ->your project in package explorer ->Buid path -> Configure Build path -> libraries

buidpath

Select your mysql connector Downloded in step 3 above -> Click ok -> Run Project.

SDL Lab Assignments

Data Structures in java

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;

 

public class DataStructures {

enum Days //to define fixed set of constants static and final
{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;

}
public static void main(String[] args) throws IOException
{
Enumeration days; //to iterate elements in a collection of objects
Vector dayNames = new Vector();
dayNames.add(“Sunday”);
dayNames.add(“Monday”);
dayNames.add(“Tuesday”);
dayNames.add(“Wednesday”);
dayNames.add(“Thursday”);
dayNames.add(“Friday”);
dayNames.add(“Saturday”);
days = dayNames.elements();

while(days.hasMoreElements())
{
System.out.println(days.nextElement());
}

Days day=Days.Monday;

switch(day)
{
case Sunday :
System.out.println(“sunday”);
break;
case Monday :
System.out.println(“monday”);
break;
}

//bit set special type of array that holds bit values can increase in size as needed.

BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();

for(int i=1;i<=10;i++)
{
if(i%2==0)
bits1.set(i);
else
bits2.set(i);
}

System.out.println(bits1);
System.out.println(bits2);

//and op
System.out.print(“bits and”);
bits2.and(bits1);
System.out.println(bits2);

//or op
System.out.print(“bits or”);
bits2.or(bits1);
System.out.println(bits2);

//vactor
dynamic array
Vector<String> v=new Vector<>();
v.add(“hi”);
v.add(“hello”);
v.add(“ok”);

for(String s:v)
{
System.out.println(s);
}

//stack

Stack st=new Stack();
st.push(“hi”);
st.push(“hello”);
st.push(“bye”);

System.out.println(st.pop());
System.out.println(st);

//dictionary

//key value
Dictionary d = new Hashtable();
d.put(1, “sunday”);
d.put(2, “monday”);
d.put(3, “tuesday”);
d.put(4, “wednesday”);
d.put(5, “thursday”);
d.put(6, “friday”);
d.put(7, “saturday”);

for(int i=1;i<d.size();i++)
{
System.out.println(d.get(i));
}

Enumeration e1 = d.keys();
Enumeration e2 = d.elements();
while(e1.hasMoreElements())
{
System.out.println(e1.nextElement());
}
while(e2.hasMoreElements())
{
System.out.println(e2.nextElement());
}
//dictionary -> HashTable ->Properties

//properties key value both strings

Properties capitals = new Properties();
capitals.put(“India”, “Delhi”);
capitals.put(“United States”, “Washington D.C.”);
capitals.put(“United Kingdom”, “London”);
capitals.put(“Japan”, “Tokyo”);
capitals.put(“Thailand”, “Bangkok”);
capitals.put(“France”, “Paris”);
capitals.put(“Germany”, “Berlin”);

Set keys=capitals.keySet();
Iterator itr = keys.iterator();

while(itr.hasNext()) {
String str = (String) itr.next();
System.out.println(“The capital of ” + str + ” is ” +
capitals.getProperty(str) + “.”);
}

capitals.store(new FileWriter(“info.properties”),”Properties Example”);

FileReader reader=new FileReader(“info.properties”);

Properties p=new Properties();
p.load(reader);

System.out.println(p.getProperty(“India”));
System.out.println(p.getProperty(“United States”));
}
}