Java Swing Project - Swing Project in Java
Database Code
sp_adduserdata
ALTER procedure [dbo].[sp_adduserdata]
(
@name varchar(100),
@uname varchar(100),
@pwd varchar(100)
)
as begin
--- database validation
insert into tbl_user(name, uname,pwd)
values(@name,@uname,@pwd)
end
sp_getuserdata
ALTER procedure [dbo].[sp_getuserdata]
as begin
select * from tbl_user
end
sp_loginuser
ALTER procedure [dbo].[sp_loginuser]
(
@uname varchar(100),
@pwd varchar(100)
)
as begin
select name from tbl_user
where uname = @uname and pwd=@pwd
end
sp_searchuserdata
ALTER procedure [dbo].[sp_searchuserdata]
(
@searchdata varchar(100)
)
as begin
select * from tbl_user where uname like '%'+@searchdata+'%'
end
Libraries
Download the Java Swing Project Libraries : Click Here


To Create a Project, File -> new project -> Under categories - Choose java with ant-under projects select java application and click on next.

Under Name and location->select the project name as->Bottle Manufacturing and select create main class check box and click on next Project will be created.

- mssql-jdbc-11.2.0.jre18(download latest version)- to connect with sql server database.
- mysql-connector- to connect to mysql server.
- rs2xml-to create data in the table format from sql in grid.
- sqljdbc4.2.0-for sql java database connectivity.
After downloading the library files -> under project explorer choose your project name. for ex Bottle Manufacturing -> look out for libraries tab - Right click--> select add jar/folder to add the libraries to your project.

Expand libraries tab-> verify if all the above libraries are added.

To create a Jframe form: Choose the project package - >new ---> jframeform --->New Jframe form windows pop up - Choose the class name as Login-----> click Finish.


In login.java jframe form ----->Drag and drop a label field from the palette option from the JFrame and rename it to Login Form.

Right click on the label field ----> Select Properties ---> font ---->segoe ui 24 plain.

In the Jframe ----> Drag and drop two labels and corresponding text boxes and change the name of the label to username and password. For user name text box, Right Click text box----under Change variable name - > Rename the text box name to txt_login.

For password text box, Right Click text box----under change variable name ->Rename the text box name to tbl_pwd.

After changing the text box variable names, drag and drop two buttons from the palette and rename them to submit and cancel. Desigining of Jframe login.java is finished.

Under source -> below the package bottle_manufacturing --> Import all the necessary library files required to run the project.

Step to write the code for submit button when clicking of button action is performed:

Login.java
package bottlemanufacturing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author DELL
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txt_login = new javax.swing.JTextField();
txt_pwd = new javax.swing.JTextField();
btn_submit = new javax.swing.JButton();
btn_Cancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N
jLabel1.setText("Login Form");
jLabel2.setText("User Name");
jLabel3.setText("Password");
btn_submit.setText("Submit");
btn_submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_submitActionPerformed(evt);
}
});
btn_Cancel.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btn_submit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn_Cancel))
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txt_login)
.addComponent(txt_pwd, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(117, 117, 117))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel1)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addComponent(txt_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txt_pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_submit)
.addComponent(btn_Cancel))
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
PreparedStatement pst=null;
private void btn_submitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
String uname = txt_login.getText();
String pwd = txt_pwd.getText();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("connection done");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-0N2GOP3\\SQLEXPRESS;encrypt=true;TrustServerCertificate=true;databaseName=bottle_manufacturing_March-23","nirmal","nirmal");
String sql = "exec sp_loginuser ?,?";
System.out.println(sql);
pst = conn.prepareStatement(sql);
pst.setString(1, uname);
pst.setString(2, pwd);
ResultSet rs= pst.executeQuery();
if (rs.next())
{
String loggedinUser = rs.getString("name");
System.out.println("The user is " + loggedinUser);
JOptionPane.showMessageDialog(null, "Valid User. ");
dispose();
Dashboard dashBoard=new Dashboard();
dashBoard.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Invalid User. Please try again");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Invalid User. Please try again");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_Cancel;
private javax.swing.JButton btn_submit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txt_login;
private javax.swing.JTextField txt_pwd;
// End of variables declaration
}







NewUserForm.java
package bottlemanufacturing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author DELL
*/
public class NewUserForm extends javax.swing.JFrame {
/**
* Creates new form NewUserForm
*/
public NewUserForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txt_login = new javax.swing.JTextField();
txt_pwd = new javax.swing.JTextField();
btn_submit = new javax.swing.JButton();
btn_Cancel = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
txt_name = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N
jLabel1.setText("New User Form");
jLabel2.setText("User Name");
jLabel3.setText("Password");
btn_submit.setText("Submit");
btn_submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_submitActionPerformed(evt);
}
});
btn_Cancel.setText("Cancel");
jLabel4.setText("Name");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(79, 79, 79))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(157, Short.MAX_VALUE)
.addComponent(btn_submit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn_Cancel))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txt_name, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txt_login)
.addComponent(txt_pwd, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)))))
.addGap(87, 87, 87))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addComponent(txt_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txt_pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_submit)
.addComponent(btn_Cancel))
.addGap(54, 54, 54))
);
pack();
}//
PreparedStatement pst=null;
private void btn_submitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
String name = txt_name.getText();
String uname = txt_login.getText();
String pwd = txt_pwd.getText();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("connection done");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-0N2GOP3\\SQLEXPRESS;encrypt=true;TrustServerCertificate=true;databaseName=bottle_manufacturing_March-23","nirmal","nirmal");
String sql = "exec sp_adduserdata ?,?,?";
System.out.println(sql);
pst = conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, uname);
pst.setString(3, pwd);
int i= pst.executeUpdate();
if (i==0)
{
JOptionPane.showMessageDialog(null, "Data not interserted");
}
else
{
JOptionPane.showMessageDialog(null, "Valid User. ");
dispose();
Login login=new Login();
login.setVisible(true);
}
}
catch(Exception ex)
{
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewUserForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_Cancel;
private javax.swing.JButton btn_submit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField txt_login;
private javax.swing.JTextField txt_name;
private javax.swing.JTextField txt_pwd;
// End of variables declaration
}


Dashboard.java
package bottlemanufacturing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
/**
*
* @author DELL
*/
public class Dashboard extends javax.swing.JFrame {
/**
* Creates new form Dashboard
*/
public Dashboard() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tbl_showData = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tbl_showData.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tbl_showData);
jButton1.setText("Fetch Data");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N
jLabel1.setText("DashBoard");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(111, 111, 111)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 60, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(51, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
PreparedStatement pst1=null;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("connection done");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-0N2GOP3\\SQLEXPRESS;encrypt=true;TrustServerCertificate=true;databaseName=bottle_manufacturing_March-23","nirmal","nirmal");
String sql = "exec sp_getuserdata";
pst1 = conn.prepareStatement(sql);
ResultSet rs= pst1.executeQuery();
tbl_showData.setModel(DbUtils. resultSetToTableModel(rs));
}
catch(Exception ex)
{
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Dashboard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tbl_showData;
// End of variables declaration
}
