Lesson-24 - global infotech

advertisement
L24
1 of 3
JDBC
JDBC stands for Java Database Connectivity . It is a set of Java APIs used for executing
SQL statements. This API consists of a set of classes and interfaces to enable
programmers to write Java Database applications.
Application
s
JDBC
API
NETWORK
Interface
Network
Database Server
Database
Characteristics of JDBC
It is a call-level SQL interface for Java
It does not restrict the type of queries passed to an underlying
DBMS driver
JDBC mechanisms are simple to understand and use
It provides a Java interface that stays consistent with the rest of
the Java system
JDBC may be implemented on top of common SQL level APIs
It uses strong , static typing wherever possible
Java and JDBC
The combination of Java JDBC is very useful because it lets the
programmer run his/her program in different platform.
Advantages of JDBC
Easy and economical
Continued usage of already installed databases
Development time is short
Installation and version control simplified
JDBC does following 3 things
Establish connection with a database
Send SQL statements
Process the results
The java.sql package
Core Java
GLOBAL INFOTECH
L24
2 of 3
We have seen that the JDBC API defines a set of interfaces and
classes to be used for communicating with a database , These interfaces and classes are
found in the java.sql package.
SQLException
The SQLException extends the exception Exception . The
SQLException provides information on a database access error. The information given
in the exception includes a string describing the error , a string describing the SQLState ,
the error code and a chain to the next exception.
To run the following example create a database and table in MS Access and insert data
in table , then create system DSN for it .
Exp1.
Select all data from table
import java.sql.*;
class Demo
{
public static void main(String s[])
{
try
{
System.out.println("Connecting to database");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("database");
Connection cn=DriverManager.getConnection
("jdbc:odbc:ww","root","global");
// Connection cn=DriverManager.getConnection
("jdbc:odbc:ww","scott","tiger");
// Connection cn=DriverManager.getConnection
("jdbc:odbc:ww"," "," ");
// ww is a DSN name
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from pp");
while(rs.next())
{
String ss=rs.getString(1);
String ps=rs.getString(2);
System.out.println("\n " + ss);
System.out.print(" " + ps);
}
}catch(Exception e)
{
System.out.println("Sorry" + e);
}
}
}
Core Java
GLOBAL INFOTECH
L24
3 of 3
Exp2. Connect with MySQL
import java.sql.*;
public class pp
{
public static void main(String[] args) {
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root",
"server");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("select * from aaa");
while(rs.next())
{
String ss=rs.getString(1);
String ps=rs.getString(2);
System.out.println("\n " + ss);
System.out.print(" " + ps);
}
}catch(Exception e)
{
System.out.println("Sorry" + e);
}
}
}
Exp3. Insert Data into Table with Forms in MySQL .
import
import
import
import
javax.swing.*;
java.awt.*;
java.sql.*;
java.awt.event.*;
class form extends JFrame
{
JTextField text1,text2;
JLabel label1,label2;
JButton button,button1;
form()
{
setLayout(null);
text1=new JTextField(15);
text2=new JTextField(15);
Core Java
GLOBAL INFOTECH
L24
4 of 3
label1=new JLabel("ID");
label2=new JLabel("Name");
button=new JButton("Save");
button1=new JButton("Exit");
label1.setBounds(350,100,100,20);
text1.setBounds(450,100,200,20);
label2.setBounds(350,120,100,10);
text2.setBounds(450,130,200,20);
button.setBounds(350,280,100,20);
button1.setBounds(450,280,100,20);
add(label1);
add(text1);
add(label2);
add(text2);
add(button);
add(button1);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String a=text1.getText();
String b=text2.getText();
Connection con=null;
String url="jdbc:mysql://localhost:3306/";
String db="parmeet";
String driver="com.mysql.jdbc.Driver";
String user="root";
String pass="server";
try
{
Class.forName(driver);
con=DriverManager.getConnection(url+db,user,pass);
Statement st=con.createStatement();
int i=st.executeUpdate("insert into pp
values('"+a+"','"+b+"')");
JOptionPane.showMessageDialog(null,"Data issaved");
}catch(Exception e) { }
}
});
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
setVisible(false);
}
});
setSize(1024,768);
Core Java
GLOBAL INFOTECH
L24
5 of 3
setVisible(true);
}
public static void main(String args[])
{
new form();
}
}
Exp4. Save data with Forms in MS Access
import
import
import
import
javax.swing.*;
java.awt.*;
java.sql.*;
java.awt.event.*;
class form2 extends JFrame
{
JTextField text1,text2;
JLabel label1,label2;
JButton button,button1;
form2()
{
setLayout(null);
text1=new JTextField(15);
text2=new JTextField(15);
label1=new JLabel("ID");
label2=new JLabel("Name");
button=new JButton("Save");
button1=new JButton("Exit");
label1.setBounds(350,100,100,20);
text1.setBounds(450,100,200,20);
label2.setBounds(350,120,100,10);
text2.setBounds(450,130,200,20);
button.setBounds(350,280,100,20);
button1.setBounds(450,280,100,20);
add(label1);
add(text1);
add(label2);
add(text2);
add(button);
add(button1);
button.addActionListener(new ActionListener()
Core Java
GLOBAL INFOTECH
L24
6 of 3
{
public void actionPerformed(ActionEvent ae)
{
String a=text1.getText();
String b=text2.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:ww","","");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into pp
values('"+a+"','"+b+"')");
JOptionPane.showMessageDialog(null,"Data issaved");
}catch(Exception e) { }
}
});
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
setVisible(false);
}
});
setSize(1024,768);
setVisible(true);
}
public static void main(String args[])
{
new form2();
}
}
Exp5.
Delete data from Table
import java.sql.*;
class Demo
{
public static void main(String s[]) throws SQLException
{
try
{
Core Java
GLOBAL INFOTECH
L24
7 of 3
System.out.println("Connecting to database");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("database");
Connection cn=DriverManager.getConnection
("jdbc:odbc:ww","root","global");
// ww is a DSN name
Statement st=cn.createStatement();
}
Core Java
st.executeUpdate("delete from pp where id=4");
System.out.println("One row deleted");
st.close();
cn.close();
}catch(Exception ex)
{
System.out.println("The Exception raised is "+ex);
}
}
GLOBAL INFOTECH
Download