Create database with name "employeedb"
Create table with name "employee"
CREATE TABLE `employee` (
`empname` varchar(45) NOT NULL,
`empdesignation` varchar(45) NOT NULL,
`empsalary` varchar(45) NOT NULL
);
java code:
package mysqlconnectionusingarraylist;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
/**
*
* @author Aravind Sankaran Nair
*/
public class MySqlConnectionUsingArrayList {
public static void main(String[] args) {
ArrayList<Employee> employeeList =new ArrayList<Employee>();
String url = "jdbc:mysql://localhost:3306/employeedb";
try{
Connection conn = DriverManager.getConnection(url, "root", "root");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM employee");
while(rs.next()){
Employee employee=new Employee();
employee.setEmpName(rs.getString("empname"));
employee.setEmpDesignation(rs.getString("empdesignation"));
employee.setEmpSalary(rs.getString("empsalary"));
employeeList.add(employee);
}
for(int i=0;i<employeeList.size();i++){
System.out.println("Employee name is "+employeeList.get(i).getEmpName());
System.out.println("Employee designation is "+employeeList.get(i).getEmpDesignation());
System.out.println("Employee salary is "+employeeList.get(i).getEmpSalary());
System.out.println("");
}
}catch(Exception e){
}
}
}
class Employee{
private String empName;
private String empDesignation;
private String empSalary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpDesignation() {
return empDesignation;
}
public void setEmpDesignation(String empDesignation) {
this.empDesignation = empDesignation;
}
public String getEmpSalary() {
return empSalary;
}
public void setEmpSalary(String empSalary) {
this.empSalary = empSalary;
}
}
output:
Employee name is aravind
Employee designation is software engineer
Employee salary is 25000
Employee name is ambili
Employee designation is software engineer
Employee salary is 35000
Create table with name "employee"
CREATE TABLE `employee` (
`empname` varchar(45) NOT NULL,
`empdesignation` varchar(45) NOT NULL,
`empsalary` varchar(45) NOT NULL
);
java code:
package mysqlconnectionusingarraylist;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
/**
*
* @author Aravind Sankaran Nair
*/
public class MySqlConnectionUsingArrayList {
public static void main(String[] args) {
ArrayList<Employee> employeeList =new ArrayList<Employee>();
String url = "jdbc:mysql://localhost:3306/employeedb";
try{
Connection conn = DriverManager.getConnection(url, "root", "root");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM employee");
while(rs.next()){
Employee employee=new Employee();
employee.setEmpName(rs.getString("empname"));
employee.setEmpDesignation(rs.getString("empdesignation"));
employee.setEmpSalary(rs.getString("empsalary"));
employeeList.add(employee);
}
for(int i=0;i<employeeList.size();i++){
System.out.println("Employee name is "+employeeList.get(i).getEmpName());
System.out.println("Employee designation is "+employeeList.get(i).getEmpDesignation());
System.out.println("Employee salary is "+employeeList.get(i).getEmpSalary());
System.out.println("");
}
}catch(Exception e){
}
}
}
class Employee{
private String empName;
private String empDesignation;
private String empSalary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpDesignation() {
return empDesignation;
}
public void setEmpDesignation(String empDesignation) {
this.empDesignation = empDesignation;
}
public String getEmpSalary() {
return empSalary;
}
public void setEmpSalary(String empSalary) {
this.empSalary = empSalary;
}
}
output:
Employee name is aravind
Employee designation is software engineer
Employee salary is 25000
Employee name is ambili
Employee designation is software engineer
Employee salary is 35000
No comments:
Post a Comment