package com.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.driver.OracleDriver;
import com.dao.SelectDao;
import com.entity.Area;
import com.entity.City;
import com.entity.Province;
public class SelectDaoImpl implements SelectDao{
private Connection connection=null;
private PreparedStatement pstmt=null;
@Override
public List<Province> selectProvinces() {
List<Province> provinces=new ArrayList<Province>();
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="ccdust";
String password="asdfgo";
DriverManager.registerDriver(new OracleDriver());
connection=DriverManager.getConnection(url,username,password);
pstmt=connection.prepareStatement("select * from province");
rs=pstmt.executeQuery();
while(rs.next()){
Province p=new Province();
p.setPid(rs.getInt("pid"));
p.setPname(rs.getString("pname"));
provinces.add(p);
System.out.println(p.getPname());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return provinces;
}
@Override
public List<City> selectCitys() {
List<City> cities=new ArrayList<City>();
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="ccdust";
String password="asdfgo";
DriverManager.registerDriver(new OracleDriver());
connection=DriverManager.getConnection(url,username,password);
pstmt=connection.prepareStatement("select * from city");
rs=pstmt.executeQuery();
while(rs.next()){
City c=new City();
c.setPid(rs.getInt("pid"));
c.setCid(rs.getInt("cid"));
c.setCname(rs.getString("cname"));
cities.add(c);
System.out.println(c.getCname());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cities;
}
public List<City> selectCitysByPid(int pid) {
List<City> cities=new ArrayList<City>();
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="ccdust";
String password="asdfgo";
DriverManager.registerDriver(new OracleDriver());
connection=DriverManager.getConnection(url,username,password);
pstmt=connection.prepareStatement("select * from city where pid=?");
pstmt.setInt(1, pid);
rs=pstmt.executeQuery();
while(rs.next()){
City c=new City();
c.setPid(rs.getInt("pid"));
c.setCid(rs.getInt("cid"));
c.setCname(rs.getString("cname"));
cities.add(c);
System.out.println(c.getCname());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cities;
}
@Override
public List<Area> selectAreas() {
// TODO Auto-generated method stub
return null;
}
public List<Area> selectAreasByCid(int cid,int pid) {
List<Area> areas=new ArrayList<Area>();
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="ccdust";
String password="asdfgo";
DriverManager.registerDriver(new OracleDriver());
connection=DriverManager.getConnection(url,username,password);
pstmt=connection.prepareStatement("select * from area where cid=? and pid=?");
pstmt.setInt(1, cid);
pstmt.setInt(2, pid);
rs=pstmt.executeQuery();
while(rs.next()){
Area area=new Area();
area.setAid(rs.getInt(1));
area.setCid(rs.getInt(2));
area.setPid(rs.getInt(3));
area.setAname(rs.getString(4));
areas.add(area);
System.out.println(area.getAname());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return areas;
}
}
- 1
- 2
- 3
前往页