New Text Document

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

This page is for Practical Paper +2 II year.

Initial preparation for Project, Java Netbean as Front end and MySQL as backend
Down load the driver from the link
-https://dev.mysql.com/downloads/connector/j/5.1.html
Extract the driver zip folder and copy the file �mysql-connector-java-5.1.45-
bin.jar� to the below path:
C:\Program Files\Java\jdk1.7.0_01\jre\lib\ext
Database creation:
Create a database in MySQL command line.
Mysql> create database student;
Mysql> use student
Mysql> create table stu (name varchar(40), rno int NOT NULL , PRIMARY KE(rno) );

Design of Project:

Code For Add Button:


Double click on Add button and type the following code:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student","root","rudra123");
//here student is database name, root is username and rudra123 is password
Statement stmt=con.createStatement();

String sql="insert into stu values('"+jTextField1.getText()


+"',"+jTextField2.getText()+");";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"Record Inserted Successfully...");
jTextField1.setText("");
jTextField2.setText("");
stmt.close();
con.close();
}
catch(Exception e)
{ JOptionPane.showMessageDialog(null,"Error in connectivity");
JOptionPane.showMessageDialog(null, e);
}
Code For Search Button:
Double click on Search button and type the following code:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student","root","rudra123");
//here student is database name, root is username and rudra123 is password
Statement stmt=con.createStatement();

String sql1 = "select * from stu where rno='"+jTextField2.getText()+ "';";


ResultSet rs=stmt.executeQuery(sql1);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Record found! ");
jTextField1.setText(rs.getString(1));
jTextField2.setText(String.valueOf(rs.getInt(2)));
jTextField1.setEditable(true);
jTextField2.setEditable(true);
}
else
{
JOptionPane.showMessageDialog(null,"Record not found...");
rs.close();
stmt.close();
con.close();
} }
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connectivity");
JOptionPane.showMessageDialog(null, e);
}
Code For Update Button:
Double click on Update button and type the following code:
if(jTextField2.getText().equals(""))
{ JOptionPane.showMessageDialog(null,"No Record to Update...");}
else
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student","root","rudra123");
//here student is database name, root is username and rudra123 is password
Statement stmt=con.createStatement();
String sql2="update stu set rno = '"+jTextField2.getText()
+"',name='"+jTextField1.getText()+"' where rno= '"+jTextField2.getText()+"';";
stmt.executeUpdate(sql2);
JOptionPane.showMessageDialog(null,"Record Updated! ");
jTextField1.setText("");
jTextField2.setText("");
stmt.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connectivity");
JOptionPane.showMessageDialog(null, e); //optional statement to view the error
type
} }
Code For Delete Button:
Double click on Delete button and type the following code:
try{
Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student","root","rudra123");
//here student is database name, root is username and rudra123 is password
Statement stmt=con.createStatement();
String sql3="delete from stu where rno='"+jTextField2.getText()+"';";
stmt.executeUpdate (sql3);
jTextField1.setText("");
jTextField2.setText("");
JOptionPane.showMessageDialog(null,"Record Deleted....");
stmt.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connectivity");
JOptionPane.showMessageDialog(null, e); //optional statement to view the error
type
}
Clear All record in a Table:
DefaultTableModel model = (DefaultTableModel)DTable.getModel();
int rows=model.getRowCount();
if(rows>0)
{ for(int i=0;i<rows;i++)
{ model.removeRow(0);
}}}
Show All record in a Table:
//btncls.doClick();
DefaultTableModel model = (DefaultTableModel)DTable.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student","root","rudra123");
//here student is database name, root is username and rudra123 is password
Statement stmt=con.createStatement();
String query="select * from stu";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
model.addRow(new Object[]{ rs.getString(1),rs.getInt(2)});
} rs.close();
stmt.close();
con.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null,"Error in connectivity");
JOptionPane.showMessageDialog(null, e);
}}

You might also like