版权声明:java洪君 https://blog.csdn.net/qq_43532342/article/details/85066598
jdbc是经典的数据库链接方式
导入jar包,即链接包即可。在框架中,框架可以轻易做到连接数据库,拿取数据。
oracle版:
package com.hc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BaseDao {
// OracleDriver
private final static String fname="oracle.jdbc.driver.OracleDriver";
private final static String url="jdbc:oracle:thin:@localhost:1521:orcl";
static {
try {
Class.forName(fname);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getCon(){
Connection con=null;
try {
con=DriverManager.getConnection(url,"scott","tiger");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void myClose(Connection con,Statement pre){
try {
if (pre!=null) {
pre.close();
}
if (con!=null&&!con.isClosed()) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void myClose(Connection con,Statement pre,ResultSet res){
try {
if (res!=null) {
res.close();
}
if (pre!=null) {
pre.close();
}
if (con!=null&&!con.isClosed()) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
dao方法:
package com.hc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hc.entity.Area;
import com.hc.entity.City;
import com.hc.entity.Province;
public class HcDao {
public List<Province> getAllPro() {
List<Province> ml=new ArrayList<Province>();
Connection con=BaseDao.getCon();
PreparedStatement pre=null;
ResultSet res=null;
try {
pre=con.prepareStatement("select * from hat_province");
res=pre.executeQuery();
while (res.next()) {
Province p=new Province();
p.setPid(res.getString(2));
p.setPname(res.getString(3));
ml.add(p);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
BaseDao.myClose(con, pre, res);
}
return ml;
}
public List<City> getCityById(String father) {
List<City> ml=new ArrayList<City>();
Connection con=BaseDao.getCon();
PreparedStatement pre=null;
ResultSet res=null;
try {
pre=con.prepareStatement("select * from hat_city where father=?");
pre.setString(1, father);
res=pre.executeQuery();
while (res.next()) {
City p=new City();
p.setCid(res.getString(2));
p.setCname(res.getString(3));
ml.add(p);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
BaseDao.myClose(con, pre, res);
}
return ml;
}
public List<Area> getAreaById(String father) {
List<Area> ml=new ArrayList<Area>();
Connection con=BaseDao.getCon();
PreparedStatement pre=null;
ResultSet res=null;
try {
pre=con.prepareStatement("select * from hat_area where father=?");
pre.setString(1, father);
res=pre.executeQuery();
while (res.next()) {
Area p=new Area();
p.setAname(res.getString(3));
ml.add(p);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
BaseDao.myClose(con, pre, res);
}
return ml;
}
}
熟悉的感觉,但是这些都只是曾经的过往,框架才是主流!