Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts

Thursday, 22 October 2015

, ,

Employee Management Project In Java

This project completely  based on jdbc and jswing.This is a perfect project for beginners who want to learn database management system in java.Following are the features of this project.

FEATURES:

Save employee record.
It can calculate salary of employee.
It can search employee record.
It can delete employee record.

OUTPUT: 





DOWNLOAD THIS PROJECT:


                           DOWNLOAD


Saturday, 17 October 2015

,

How To Load And Refresh Data Base Data In Table JavaFx

In this tutorial we are going to discussed how to load database data in table after the table have loaded sometimes data becomes change we should have a option to refresh our data in this program we have show you how to refresh table in javafx on a click.This enable user to continuously update database data.  

CODING:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
public class mybej extends Application 
{

private final TableView<myData> table = new TableView<>();
private final ObservableList<myData> data =
FXCollections.observableArrayList();
String s,s1,s2,s3,s4,s5;
Button b1 = new Button("REFRESH");
ResultSet database,database1;
Statement st,st1;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableColumn firstCol = new TableColumn("First Name");
firstCol.setMinWidth(100);
firstCol.setCellValueFactory(
new PropertyValueFactory<>("name"));
TableColumn secCol = new TableColumn("Fax-Number");
secCol.setCellValueFactory(
new PropertyValueFactory<>("number"));
TableColumn lastCol = new TableColumn("Email");
lastCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstCol, secCol,lastCol);
table.setMinWidth(230);
table.setLayoutX(10);
table.setLayoutY(40);
b1.setLayoutX(5);
b1.setLayoutY(5);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st1=cn.createStatement();
database=st1.executeQuery("select * from load");
while(database.next())
{
s = database.getString(1);
s1 = database.getString(2);
s2 = database.getString(3);
data.add(new myData(s,s1,s2));
}  
st.close();
database.close();
cn.close();
}
catch(Exception e)
{
System.out.print(e);

}
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override 
public void handle(ActionEvent e) {
try
{
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();

database=st1.executeQuery("select * from load");
data.removeAll(data);
while(database.next())
{
s3 = database.getString(1);
s4 = database.getString(2);
s5 = database.getString(3);

data.add(new myData(s3,s4,s5));

}
st.close();
cn.close();
}   catch(Exception ex)
{
System.out.print(ex);
}
}
});
Group root = new Group();
root.getChildren().addAll(table,b1);
stage.setTitle("Table with Database");    
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}

public static class myData {

private final SimpleStringProperty Name;
private final SimpleStringProperty email;
private final SimpleStringProperty number;
private myData(String fName,String fnum,String email) {
this.Name = new SimpleStringProperty(fName);
this.email = new SimpleStringProperty(email);
this.number = new SimpleStringProperty(fnum);
}


public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public String getName() {
return Name.get();
}
public void setName(String fName) {
Name.set(fName);
}
public String getNumber() {
return number.get();
}
public void setNumber(String fName) {
number.set(fName);
}

}
} 

OUTPUT:





, ,

How To Create A Login Frame With Create New Id Option In Java

In this tutorial we are going to create a login frame using jdbc  in this login user can create a new his/her id with password.Let's start first create a new project in netbeans then create new jframe form.


FINAL RESULT:


CREATING JPANEL:

Change layout of frame by pressing right click on frame then select layout>>Flowlayout.After this pick and drop jpanel in frame from palette.Change the colour of  jpanel to make it attractive.

DESIGNING MAIN WINDOW:

Our next step is to create main window for our project for this following controls.
  1. Pick two label and drop in frame and edit text of label as shown in pictures.
  2. Select jtextfield and change it variable name to userfield.
  3.  Create jpasswordfield and change its vraible name to passfield.
  4. Then create a button and change it's variable name and text to login.
  5. Finally create a button for a register a new user option change it's variable name as register.
After creating controls design your frame as shown in below picture by changing font style and colour of label and jbutton text change layout of your frame to make it attractive.


DESIGNING DIALOG BOX :

As a user click on click new id option new dialog box should be open so we have to create a dialog box for our login frame to create dialog box  press right click on other component in navigator as shown in picture.



After this step you will see that dialog box will appear above frame in navigator  change its variable name to box press double click on it to design now pick following components from palette:
  1. Three labels and change its text.
  2. One jtextfield and change it's variable as newid.
  3. Now Create jpasswordfield and change it's variable name as newpass
  4. Create jbutton and change its variable and text as create.
Our dialog box is completed set layout and design of dialog box as shown in below picture.


To make it accessible with our main frame double click on register button and paste a below code.
  box.setVisible(true);

CREATE DATA BASE FOR LOGIN FRAME:

For this project we need a database for this frame I have created database in ms access.your next step is to create a table in ms access your table name should be logtab. Then create a data source your data source name should be login.Your table should contain two columns as shown in below picture.






CREATING METHOD FOR CREATE OPTION:

In this step we will code for create a new id option you have to simply paste a below code below constructor in code area 

public void  create(String a,String b)
{
try{
Statement st;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();
st.executeUpdate("INSERT into logtab VALUES('"+a+"','"+b+"')");
JOptionPane.showMessageDialog (null,"New ID is Succesfully Created", "Message", JOptionPane.INFORMATION_MESSAGE);

box.dispose();

}
catch(Exception e)
{
JOptionPane.showMessageDialog (null, e, "Error", JOptionPane.ERROR_MESSAGE);
}


}    

When a user clicks on create button above method should be called so double click on create button then copy below code in action performed method of create button.


String nid=newid.getText();

String pass=newpass.getText(); 
create(nid,pass);

CREATING METHOD FOR LOGIN OPTION:

In last create method for login option when user clicks on login below method should be call so paste below code after first method 
public void match(String a,String b) 
{
Boolean c = false,d=false;
try {
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
Statement st=cn.createStatement();
ResultSet data,data1;
data=st.executeQuery("select * from logtab where id='"+a+"'");
if(data.next())
{
c = true;
}
else{
System.out.print("ID Not Matched ");
}
data1=st.executeQuery("select * from logtab where password='"+b+"'");
if(data1.next())
{
d = true;
}
else
{
System.out.println("password not match");
}
if(c & d)
{
System.out.print("Successfully Login ");
} 
} 
catch(Exception e)
{
System.out.print(e);
}
}

after copying this code paste below code in login action performed method code 
String nid=newid.getText(); 
String pass=newpass.getText();
 create(nid,pass);

Our project is successfully created must share your feedback about this project.


Tuesday, 13 October 2015

How To Create Database Of Java Application In Microsoft Access

This tutorial will show you how to create database in microsoft access of your java application using jdbc.

HOW TO CREATE TABLE IN MS ACCESS:

Open microsoft access and new blank desktop database then select create option then TableDesign empty table will be created as shown in below picture.


After this fill this table according to your need and set data type as long text or short text as shown in below picture.


Then press cntrl+s to save your table and change table name with data because this name will be used in code.Our table is successfully created.In next step save ms access file in your project folder,save database file name as data and during saving you file select access 2000 database as a file type.



CREATING DATA SOURCE FOR DATA BASE:

In order to connect our application with  database we require a dsn so create a dsn.Go to control panel>>Administrative tools then open ODBC Data Sources window will open like this


Now click on add button on this window to create DSN now new window will open select microsoft access driver option as shown in below picture.


Now select your ms access file from where you have saved change data source name with login.After selecting finish button our database will created our next step is to check whether out database is working fine or not.

HOW TO CHECK JDBC CONNECTION:

We have created our database our last step is to check our database connection is fine or not create a new java file and paste below code in it if your database connection is fine it will print Successfully  Connected with database

import java.sql.*;

public class projava {



public static void main(String[] args) {


try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
cn.close();
System.out.print("Successfully Connected with database");
}
catch (Exception ex)
{
System.out.print(ex);
}
}

}

Sunday, 13 September 2015

,

How To Load DataBase Data In Jtable

When dealing with database programs we often need to load or connect our database data in jtable in these tutorial we showed how to load whole database data in jtable.In  the following program we have used following methods:

RELATED TUTORIALS:

ResultSet ExecuteQuery:

As you can see its return type is resultset so it returns resultset it has been used with select statement.

GetString:

This method is used to get string from specified column.

CODING:

import javax.swing.*; 
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.DefaultTableModel;

class myjframe1 extends JFrame
{
DefaultTableModel model;
String rowData[][];
JTable table;
String s,s1,s2;
public myjframe1()
{

setTitle("Loading DataBase Data");
setSize(300, 200);

setDefaultCloseOperation(EXIT_ON_CLOSE);      

String columnNames[] = { "NAME", "EMAIL", "PHONE NUMBER" };


model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
setLayout(new FlowLayout(FlowLayout.CENTER));
JScrollPane js = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(js);
create();
setVisible(true);

}
private  void  create()
{

try{
Statement st;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();
ResultSet data;


data=st.executeQuery("select * from load");
while(data.next())
{
s = data.getString(1);
s1 = data.getString(2);
s2 = data.getInt(3);
model.addRow(new String[]{s,s1,s2});
}
st.close();
cn.close();
data.close();

}
catch(ClassNotFoundException e)
{
System.out.print(e);

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

}

}

class pra
{
public static void main(String args[])
{
myjframe1 x= new myjframe1();
}
}

OUTPUT: