Programs Related to Database Connection with Java.
Here Is My database:
Here,Logical name is StudentDetails
Source Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import sun.jdbc.odbc.*;
/*
<applet code="DataBaseDemo" width=550 height=550>
</applet>
*/
public class DataBaseDemo extends Applet implements ActionListener
{
Statement stmt;
String s="Wel Come.";
Label Lname,LRollNo;
Button Show,Insert,Update,Del;
TextField txtName,txtRoll;
public void init()
{
Lname=new Label("name");
LRollNo=new Label("roll no");
Show=new Button("show");
Insert=new Button("insert");
Update=new Button("update");
Del=new Button("delete");
txtName=new TextField(12);
txtRoll=new TextField(8);
add(Lname);
add(txtName);
add(LRollNo);
add(txtRoll);
add(Show);
add(Insert);
add(Update);
add(Del);
Show.addActionListener(this);
Insert.addActionListener(this);
Update.addActionListener(this);
Del.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:StudentDetails","","");
stmt = conn.createStatement();
}
catch(Exception e)
{}
}
public void showData(String msg)
{
try
{
String sql = "select * from StudentDetail ";
ResultSet rs = stmt.executeQuery(sql);
s=msg;
while(rs.next())
{
s = s+ rs.getString(1)+" ";
s =s+ rs.getString(2)+" ";
}
}
catch(Exception e)
{}
}
public void actionPerformed(ActionEvent ae)
{
String m=ae.getActionCommand();
if(m.equals("show"))
{
showData("Name and Roll Number Stored in database are.. ");
}
else if(m.equals("insert"))
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "INSERT into StudentDetail VALUES ('"+name+"','"+Roll+"')";
int num= stmt.executeUpdate(sql);
showData("Inserted.New Data : ");
}
catch(Exception e)
{}
}
else if(m.equals("delete"))
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "DELETE from StudentDetail WHERE StuRoll='"+Roll+"'";
int num= stmt.executeUpdate(sql);
showData("Deleted.New Data : ");
}
catch(Exception e)
{
}
}
else
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "UPDATE StudentDetail set StuName='"+name+"' WHERE StuRoll='"+Roll+"'";
int num= stmt.executeUpdate(sql);
showData("Updated.New Data : ");
}
catch(Exception e)
{
}
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(s,10,70);
}
}
Here,Logical name is StudentDetails
Source Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import sun.jdbc.odbc.*;
/*
<applet code="DataBaseDemo" width=550 height=550>
</applet>
*/
public class DataBaseDemo extends Applet implements ActionListener
{
Statement stmt;
String s="Wel Come.";
Label Lname,LRollNo;
Button Show,Insert,Update,Del;
TextField txtName,txtRoll;
public void init()
{
Lname=new Label("name");
LRollNo=new Label("roll no");
Show=new Button("show");
Insert=new Button("insert");
Update=new Button("update");
Del=new Button("delete");
txtName=new TextField(12);
txtRoll=new TextField(8);
add(Lname);
add(txtName);
add(LRollNo);
add(txtRoll);
add(Show);
add(Insert);
add(Update);
add(Del);
Show.addActionListener(this);
Insert.addActionListener(this);
Update.addActionListener(this);
Del.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:StudentDetails","","");
stmt = conn.createStatement();
}
catch(Exception e)
{}
}
public void showData(String msg)
{
try
{
String sql = "select * from StudentDetail ";
ResultSet rs = stmt.executeQuery(sql);
s=msg;
while(rs.next())
{
s = s+ rs.getString(1)+" ";
s =s+ rs.getString(2)+" ";
}
}
catch(Exception e)
{}
}
public void actionPerformed(ActionEvent ae)
{
String m=ae.getActionCommand();
if(m.equals("show"))
{
showData("Name and Roll Number Stored in database are.. ");
}
else if(m.equals("insert"))
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "INSERT into StudentDetail VALUES ('"+name+"','"+Roll+"')";
int num= stmt.executeUpdate(sql);
showData("Inserted.New Data : ");
}
catch(Exception e)
{}
}
else if(m.equals("delete"))
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "DELETE from StudentDetail WHERE StuRoll='"+Roll+"'";
int num= stmt.executeUpdate(sql);
showData("Deleted.New Data : ");
}
catch(Exception e)
{
}
}
else
{
try
{
String name=txtName.getText();
String Roll=txtRoll.getText();
String sql = "UPDATE StudentDetail set StuName='"+name+"' WHERE StuRoll='"+Roll+"'";
int num= stmt.executeUpdate(sql);
showData("Updated.New Data : ");
}
catch(Exception e)
{
}
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(s,10,70);
}
}
- Save the file with name DataBaseDemo.java
- Complie with: javac DataBaseDemo.java
- Run with:
appletviewer -J-Djava.security.policy=mypolicy.txt DataBaseDemo.java
Here is my Policy file.save it with name mypolicy.txt
Keep it in the same folder where above program is.
grant
{permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc", "read";
permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc", "write";
permission java.util.PropertyPermission "file.encoding", "read";
permission java.util.PropertyPermission "file.encoding", "write";
};
Out Put is as following.
Out Put For Show Button:
Out Put For Insert Data Button:
Out Put For Insert Data Button:
Out Put For Insert Data Button:
No comments:
Post a Comment