Week 12 JDBC Connectivity
Week 12 JDBC Connectivity
Let's first create a table in the mysql database, but before creating table, we need
to create database first.
1. create database IETOOPLAB;
2. use IETOOPLAB;
3. create table emp(id int(10),name varchar(40),age int(3));
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/IETOOPLAB","root","root");
//here IETOOPLAB is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The above example will fetch all the records of emp table.
2) Set classpath:
There are two ways to set the classpath:
o temporary
o permanent
How to set the temporary classpath
open command prompt and write:
1. C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
Exercise:
Develop java application that connects Employee Registration Form (EMP Name, EMP
Id, EMP Address, and EMP Email) with Database.