Sie sind auf Seite 1von 5

JDBC -> Java Database Connectivity

---------
Used to connect with database..
ODBC - Open Database Connectivity
SQL - Server
-------------------
username: sa
password: killer

Create table stud(sno int,sname varchar(20),course varchar(10),fees int)


sp_help stud
Select * from stud
Insert into stud values(100,'aaaa','dast',4500);

Creating DSN (Data source Name)


-------------------------------------------
Start -> settings -> Control Panel -> Administrative Tools -> ODBC Data Sources
* Click the Add Button
* Select the Driver Name "SQL -Server"

Steps to Connect with Backend


-------------------------------------------
1. Initialize the Driver File - Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
2. Establish the connection with database.
3. Query the Database.
java.sql.*;
---------------
Connection -> Establish Connection
ResultSet -> Query the Database (Select)
Statement -> Execute the commands
(DDL(Data Definition Language) - Create, Alter, Drop
DML(Data Manipulation Language)- Insert, Update, Delete
TCL(Transaction and control Language) - commit, Rollback)
PreparedStatement -> Execute the commands.
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mysql","sa"
,"killer");
}
Connection Class Methods
---------------------------------
createStatement() -> Initialize the Statement object
Statement Class Methods
----------------------------------
executeUpdate(String qry) -> Execute Create, Alter, Drop, Insert, Update,
Delete Command
executeQuery(String qry) -> Execute Select command.
Eg:
-----
ConnectionDemo.java
-------------------------------
import java.sql.*;
public class ConnectionDemo
{
public Connection con;
public ConnectionDemo()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:mydsn","sa"
,"killer");
System.out.println("Connected");
}catch(Exception e)
{
System.out.println("Connection Error : " + e);
}
}
}
TableCreationDemo.java
------------------------------------
import java.sql.*;
class TableCreationDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:
mydsn","sa","killer");
String qry = "Create Table Stud1(sno int,sname varchar(2
0))";
Statement st = con.createStatement();
st.executeUpdate(qry);
System.out.println("Table Created");
}catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}

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=" ";