Chapter Six Java Database Connectivity: Debre Markos University Department of Computer Science
Chapter Six Java Database Connectivity: Debre Markos University Department of Computer Science
Chapter Six
Java Database Connectivity
Advanced Programming(CoSc2084)
03/18/2021
What is JDBC?
* JDBC stands for Java Database Connectivity, which is a standard Java
API for database-independent connectivity between the Java
programming language and a wide range of databases.
* The JDBC library includes APIs for each of the tasks commonly
associated with database usage:
*To load the driver, you just load the appropriate class;
a static block in the driver class itself automatically
makes a driver instance and registers it with the JDBC
driver manager.
………
*One of the beauties of the JDBC approach is that the
database server requires no changes whatsoever.
* import java.sql.*;
* To load driver go to project library and add jar file mysql
connector.
………
//STEP 2: Establish a connection
…….
ResultSetmyRs = myStmt.executeQuery(“SELECT * FROM employee”);
While (myRs.next()){
System.out.println(myRs.getString(“last_name”));
System.out.println(myRs.getString(“First_name”));
}
………
//STEP 5: Get ResultSet
String queryLehigh = "select * from Lehigh";
ResultSet rs = Stmt.executeQuery(queryLehigh);
while (rs.next()) {
}
………
rs.close();
stmt.close();
conn.close();
Sample JDBC Program
System.out.println("insert complete.");
exc.printStackTrace();
}}
2. Selection Operation
packagejdbcdemo;
importjava.sql.*;
publicclass Driver {
publicstaticvoid main(String[] args) {
try {
Connection myConn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","root");
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from employee");
while(myRs.next()){ System.out.println(myRs.getString("last_name")
+","+myRs.getString("first_name")); } }
catch (Exception exc){
exc.printStackTrace();
} }}
3. delete operation
importjava.sql.*;
public class DeleteJava {
public static void main(String[] args) {
String url= "jdbc:mysql://localhost:3306/demo";
String user = "root";
String pass= "root";
try {
//1 get a connection to database
Connection myConn = DriverManager.getConnection(url, user, pass);
//crate statement
Statement myStmt = myConn.createStatement();
//3. execute SQL insert
String sql = "delete from employee where last_name='abebe'";
Int rowsAffected=myStmt.executeUpdate(sql);
System.out.println("Rows affected:"+rowsAffected);
System.out.println("Delete complete"); }
catch (Exception exc){
exc.printStackTrace();
} }}
4. Update Operation
packagejdbcdemo;
importjava.sql.*;
publicclassUpdateStm {
publicstaticvoid main(String[] args) {
String url= "jdbc:mysql://localhost:3306/demo";
String user = "root";
String pass= "root";
try {
//1 get a connection to database
Connection myConn = DriverManager.getConnection(url, user,
pass);
//crate statement
Statement myStmt = myConn.createStatement();
………
//3. execute SQL insert
String sql = "update employee"
+" set email= 'kebede'"
+"where id=4";
myStmt.executeUpdate(sql);
System.out.println("Update complete.");
}
catch (Exception exc){
exc.printStackTrace();
}
}
}
OU
K Y
A N
TH
RY
VE
! !
C H
MU
03/18/2021