Skip to content

Commit ffd82bd

Browse files
committed
JDBC project is added
0 parents  commit ffd82bd

File tree

343 files changed

+14715
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+14715
-0
lines changed

.metadata/.lock

Whitespace-only changes.

.metadata/.log

Lines changed: 1464 additions & 0 deletions
Large diffs are not rendered by default.
560 Bytes
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.lwl.jdbc;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class JdbcExample {
10+
11+
public static void main(String[] args) {
12+
Connection conn = null;
13+
Statement st = null;
14+
ResultSet rs = null;
15+
try {
16+
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch_4", "lakshman", "lakshman");
17+
st = conn.createStatement();
18+
rs = st.executeQuery("select ename,job,sal from emp");
19+
while(rs.next()) {
20+
String name = rs.getString("ename");
21+
String job = rs.getString("job");
22+
float salary = rs.getFloat("sal");
23+
System.out.printf("%s - %s - %f",name,job,salary);
24+
}
25+
26+
} catch (SQLException e) {
27+
System.out.println("While getting employee details:"+e);
28+
e.printStackTrace();
29+
}finally {
30+
try {
31+
if(rs!=null)
32+
rs.close();
33+
if(st!=null)
34+
st.close();
35+
if(conn !=null)
36+
conn.close();
37+
}catch (Exception e) {
38+
System.out.println("While closing resources: "+e);
39+
}
40+
}
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.lwl.jdbc.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.sql.Connection;
8+
import java.sql.Statement;
9+
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.Test;
12+
13+
import com.lwl.jdbc.util.ConnectionUtil;
14+
15+
public class JDBCCurdTest {
16+
17+
@BeforeAll
18+
public static void init() {
19+
ConnectionUtil util = ConnectionUtil.util;
20+
Connection con = null;
21+
Statement st = null;
22+
23+
try {
24+
System.out.println("Init is done");
25+
con = util.getConnection();
26+
st = con.createStatement();
27+
String DROP_TABLE = "DROP TABLE IF EXISTS student";
28+
String DROP_TABLE_ADDRESS = "DROP TABLE IF EXISTS address";
29+
st.execute(DROP_TABLE);
30+
st.execute(DROP_TABLE_ADDRESS);
31+
String CREATE_STUDENT_TABLE = "create table student(sid int not null auto_increment,name varchar(100),email varchar(120),degree varchar(120),primary key(sid));";
32+
String CREATE_STUDENT_TABLE = "create table student(sid int not null auto_increment,name varchar(100),email varchar(120),degree varchar(120),primary key(sid));";
33+
st.execute(CREATE_STUDENT_TABLE);
34+
} catch (Exception e) {
35+
e.printStackTrace();
36+
} finally {
37+
util.close(st, con);
38+
}
39+
}
40+
41+
@Test
42+
public void createTableStudent() {
43+
44+
45+
}
46+
}

.metadata/.plugins/org.eclipse.core.resources/.history/13/d0ec128d35a9001a1e108810345da353

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.lwl.jdbc.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.sql.Connection;
8+
import java.sql.PreparedStatement;
9+
import java.sql.Statement;
10+
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.Test;
13+
14+
import com.lwl.jdbc.util.ConnectionUtil;
15+
16+
public class JDBCCurdTest {
17+
18+
//@BeforeAll
19+
public static void init() {
20+
ConnectionUtil util = ConnectionUtil.util;
21+
Connection con = null;
22+
Statement st = null;
23+
24+
try {
25+
System.out.println("Init is done");
26+
con = util.getConnection();
27+
st = con.createStatement();
28+
String DROP_TABLE = "DROP TABLE IF EXISTS student";
29+
String DROP_TABLE_ADDRESS = "DROP TABLE IF EXISTS address";
30+
st.execute(DROP_TABLE);
31+
st.execute(DROP_TABLE_ADDRESS);
32+
String CREATE_STUDENT_TABLE = "create table student(sid int not null auto_increment,name varchar(100),email varchar(120),degree varchar(120),primary key(sid));";
33+
String CREATE_ADDRESS_TABLE = "create table address(aid int not null auto_increment,city varchar(100),state varchar(120),sid int references student(sid),primary key(aid));";
34+
st.execute(CREATE_STUDENT_TABLE);
35+
st.execute(CREATE_ADDRESS_TABLE);
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
} finally {
39+
util.close(st, con);
40+
}
41+
}
42+
43+
//@Test
44+
public void addStudentTest() {
45+
46+
final String ADD_STUDENT = "insert into student(name,email,degree) values (?,?,?);";
47+
ConnectionUtil util = ConnectionUtil.util;
48+
Connection con = null;
49+
PreparedStatement pst = null;
50+
try {
51+
con = util.getConnection();
52+
pst = con.prepareStatement(ADD_STUDENT);
53+
pst.setString(1,"Krish");
54+
pst.setString(2, "krish@gmail.com");
55+
pst.setString(3, "BE");
56+
int count = pst.executeUpdate();
57+
assertEquals(1, count);
58+
}catch (Exception e) {
59+
e.printStackTrace();
60+
}finally {
61+
util.close(pst, con);
62+
}
63+
64+
}
65+
66+
67+
@Test
68+
public void deleteStudentTest() {
69+
70+
String name = "'Krish' or 1 = 1";
71+
final String DELETE_STUDENT = "delete from student where name="+name;
72+
ConnectionUtil util = ConnectionUtil.util;
73+
Connection con = null;
74+
Statement st = null;
75+
try {
76+
con = util.getConnection();
77+
st =con.createStatement();
78+
int count = st.executeUpdate(DELETE_STUDENT);
79+
assertEquals(1, count);
80+
}catch (Exception e) {
81+
e.printStackTrace();
82+
}finally {
83+
util.close(st, con);
84+
}
85+
86+
}
87+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.lwl.jdbc;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class JdbcExample {
10+
11+
static{
12+
try {
13+
Class.forName("com.mysql.jdbc.Driver");
14+
}catch (Exception e) {
15+
System.out.println("While loading driver :"+e);
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
Connection conn = null;
21+
Statement st = null;
22+
ResultSet rs = null;
23+
try {
24+
25+
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch_4", "lakshman", "lakshman");
26+
st = conn.createStatement();
27+
rs = st.executeQuery("select ename,job,sal from emp");
28+
while(rs.next()) {
29+
String name = rs.getString("ename");
30+
String job = rs.getString("job");
31+
float salary = rs.getFloat("sal");
32+
System.out.printf("\n%s - %s - %.2f",name,job,salary);
33+
}
34+
35+
} catch (SQLException e) {
36+
System.out.println("While getting employee details:"+e);
37+
e.printStackTrace();
38+
}finally {
39+
try {
40+
if(rs!=null)
41+
rs.close();
42+
if(st!=null)
43+
st.close();
44+
if(conn !=null)
45+
conn.close();
46+
}catch (Exception e) {
47+
System.out.println("While closing resources: "+e);
48+
}
49+
}
50+
}
51+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.lwl.ems.dao;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.List;
8+
9+
import com.lwl.ems.domain.Dept;
10+
import com.lwl.ems.domain.Employee;
11+
import com.lwl.ems.dto.SalStat;
12+
import com.lwl.ems.util.DbConnectionUtil;
13+
14+
public class EmployeeDaoImpl implements EmployeeDao {
15+
16+
private static final String DEPT_SAL_SAT = "select count(*) as count,max(sal) as max,min(sal) as min,avg(sal) as avg from emp where deptno = ?";
17+
18+
private DbConnectionUtil dbUtil = DbConnectionUtil.dbutil;
19+
20+
@Override
21+
public List<Employee> getEmployeeByDept(int deptno) {
22+
// TODO Auto-generated method stub
23+
return null;
24+
}
25+
26+
@Override
27+
public List<Employee> maxSalaryEmployees() {
28+
// TODO Auto-generated method stub
29+
return null;
30+
}
31+
32+
@Override
33+
public Dept getDeptWhichHasMaxNumberOfEmployee() {
34+
// TODO Auto-generated method stub
35+
return null;
36+
}
37+
38+
@Override
39+
public List<Employee> getEmployeesWithManagers() {
40+
// TODO Auto-generated method stub
41+
return null;
42+
}
43+
44+
@Override
45+
public SalStat getSalStatOfOrganization() {
46+
// TODO Auto-generated method stub
47+
return null;
48+
}
49+
50+
@Override
51+
public SalStat getSalStatOfDept(int deptno) {
52+
Connection con = null;
53+
PreparedStatement pst = null;
54+
ResultSet rs = null;
55+
SalStat salStat = null;
56+
try {
57+
con = dbUtil.getConnection();
58+
pst = con.prepareStatement(DEPT_SAL_SAT);
59+
pst.setInt(1, deptno);
60+
rs = pst.executeQuery();
61+
if (rs.next()) {
62+
long count = rs.getLong("count");
63+
float min = rs.getFloat("min");
64+
float max = rs.getFloat("max");
65+
float avg = rs.getFloat("avg");
66+
salStat = SalStat.builder().min(min).max(max).avg(avg).count(count).build();
67+
}
68+
} catch (SQLException e) {
69+
e.printStackTrace();
70+
}finally {
71+
dbUtil.close(rs, pst, con);
72+
}
73+
return salStat;
74+
}
75+
76+
@Override
77+
public List<String> allManagerNames() {
78+
// TODO Auto-generated method stub
79+
return null;
80+
}
81+
82+
@Override
83+
public List<Employee> whoJoinDayIs(String dayName) {
84+
// TODO Auto-generated method stub
85+
return null;
86+
}
87+
88+
@Override
89+
public int addEmployees(List<Employee> list) {
90+
// TODO Auto-generated method stub
91+
return 0;
92+
}
93+
94+
@Override
95+
public int addDepartements(List<Dept> list) {
96+
// TODO Auto-generated method stub
97+
return 0;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)