Unit - 2 JDBC
Unit - 2 JDBC
Database Connectivity
Mahesh K
Syllabi
• DATABASE CONNECTIVITY: JDBC perspectives,
JDBC program example
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.
st.close();
con.close();
}
Methods of Statement object
• executeUpdate()
– Used to execute DDL (CREATE, ALTER, and DROP).
DML (INSERT, DELETE, UPDATE, etc) and DCL
statements
– The return value of this method is the number of
rows affected.
st.close();
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
System.exit(1);
}
}
}
import java.sql.*; Ex-2: Insert into Table
import java.io.*;
i += st.executeUpdate("insert into employee values(" + eno + ", \'" + ename + "\'," + sal + ")");
if(choice.equals("N") || choice.equals("n"))
wantMore = false;
else if(choice.equals("Y") || choice.equals("y"))
wantMore = true;
else throw new IncorrectChoiceException();
}
System.out.println("Update success : "+i+" rows updated");
st.close();
con.close();
}
catch(SQLException se){
se.printStackTrace(); }
catch(IncorrectChoiceException ic){
System.out.println("Error : Incorrect choice");}
finally{
System.out.println("Total updates performed : "+i);}
}}
import java.sql.*; Ex-3: Update Table
import java.io.*;
con.commit();
con.close();
}
catch(Exception e)
{
//con.RollBack();
e.printStackTrace();
}
}
}
import java.sql.*; Ex-4: Prepared statements
import java.io.*;
public class PreparedStmts {
public static void main(String s[]) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:dsn");