Академический Документы
Профессиональный Документы
Культура Документы
---------
Used to connect with database..
ODBC - Open Database Connectivity
SQL - Server
-------------------
username: sa
password: killer
InsertionDemo.java
-----------------------------
import java.sql.*;
import java.io.*;
class InsertionDemo
{
public static void main(String args[])
{
int sno;
String sname;
String wish="y";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:
mydsn","sa","killer");
BufferedReader br = new BufferedReader(new InputStreamRe
ader(System.in));
do
{
System.out.print("Enter sno and sname : ");
sno = Integer.parseInt(br.readLine());
sname = br.readLine();
String qry = "Insert into Stud1 values(" + sno +
",'" + sname + "')";
Statement st = con.createStatement();
int r = st.executeUpdate(qry);
System.out.println(r + " row(s) Inserted");
System.out.print("Add Another Record (y/n)?");
wish = br.readLine();
}while(wish.equals("y"));
}catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}
SelectionDemo.java
-------------------------
import java.sql.*;
class SelectionDemo
{
public static void main(String args[])
{
try
{
ConnectionDemo c = new ConnectionDemo();
Statement st = c.con.createStatement();
String qry = "Select * from stud1";
ResultSet rs = st.executeQuery(qry);
while(rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2))
;
}
}catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}
Student.java
------------------
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Student extends Frame implements ActionListener
{
Label sno,sname;
TextField sn,sna;
Button ins,can;
ConnectionDemo c;
String msg = new String();
public Student(String title)
{
super(title);
setLayout(new FlowLayout());
c = new ConnectionDemo();
sno = new Label("Sno");
sname = new Label("Sname");
sn = new TextField(5);
sna = new TextField(20);
ins = new Button("Insert");
can = new Button("Cancel");
add(sno);
add(sn);
add(sname);
add(sna);
add(ins);
add(can);
ins.addActionListener(this);
can.addActionListener(this);
addWindowListener(new MWA(this));
}
public void actionPerformed(ActionEvent ae)
{
int no;
String nam;
if(ae.getSource() == ins)
{
try
{
no = Integer.parseInt(sn.getText());
nam = sna.getText();
String qry = "Insert into Stud1 Values(" + no +
",'" + nam + "')";
Statement st = c.con.createStatement();
int r = st.executeUpdate(qry);
msg = r + " row(s) Inserted";
MessageBox mb = new MessageBox(msg);
mb.setVisible(true);
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
else if(ae.getSource() == can)
{
sn.setText("");
sna.setText("");
}
}
public static void main(String args[])
{
Student s = new Student("Student Data");
s.setVisible(true);
s.setSize(400,400);
}
}
class MWA extends WindowAdapter
{
Student s;
public MWA(Student s)
{
this.s = s;
}
public void windowClosing(WindowEvent we)
{
try
{
s.dispose();
System.exit(0);
}catch(Exception e){}
}
}
class MessageBox extends Frame implements ActionListener
{
Label ms;
Button b1;
public MessageBox(String lab)
{
setLayout(new FlowLayout());
ms = new Label(lab);
b1 = new Button("ok");
add(ms);
add(b1);
setSize(200,200);
setLocation(100,100);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
dispose();
}catch(Exception e){}
}
}
Update name from stud where sno=101;
Delete from stud where name=" ";