Experiment-6 Abhishek Choudhary 07311503113 AIM
Experiment-6 Abhishek Choudhary 07311503113 AIM
Experiment-6 Abhishek Choudhary 07311503113 AIM
ABHISHEK CHOUDHARY
07311503113
AIM
Write a program of database connectivity using JDBC
(A) To display details of all employees
(B) To display details of an employee by taking employee id by user
(A) SOURCE CODE
import java.sql.*;
public class jdbc1
{
static final String
static final String
static final String
static final String
JDBC_DRIVER="com.mysql.jdbc.Driver";
DB_URL="jdbc:mysql://localhost/";
user="root";
pass="";
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT
stmt=conn.createStatement();
String sql;
sql="select * from employee where id="+sid;
ResultSet rs=stmt.executeQuery(sql);
System.out.println("-------------------------");
System.out.print("ID"+"\t");
System.out.print("NAME"+"\t");
System.out.println("SALARY");
System.out.println("-------------------------");
while(rs.next())
{
int id=rs.getInt("id");
String name=rs.getString("name");
int salary=rs.getInt("salary");
System.out.print(id);
System.out.print("\t"+name);
System.out.println("\t"+salary);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT