BaseDao
package com.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Common implements ICommon {
//driver
String classStr="oracle.jdbc.driver.OracleDriver";
//connection
String connStr="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
//username
String name="lw";
//password
String password="admin";
//transact add/delete/update method
public boolean tsg(String sql){
boolean b=false;
try {
//loading driver
Class.forName(classStr);
//create connection
Connection conn=DriverManager.getConnection(connStr,name,password);
//create transact SQL object
Statement sta=conn.createStatement();
//transact SQL
sta.executeUpdate(sql);
//close sta
sta.close();
//close conn
conn.close();
//set b true
b=true;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return b;
}
//transact select
public ResultSet select(String sql){
try {
Class.forName(classStr);
Connection conn=DriverManager.getConnection(connStr,name,password);
Statement sta=conn.createStatement();
ResultSet rs=sta.executeQuery(sql);
return rs;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
DemoDaoImpl
package com.demo.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.demo.bean.Dept;
import com.demo.common.BaseDaoImpl;
import com.demo.common.IBaseDao;
public class DemoDaoImpl implements IDemoDao {
private IBaseDao baseDao = new BaseDaoImpl();
public Dept queryDemoById(int id) {
String sql = "select * from dept where did=" + id;
ResultSet rs = baseDao.query(sql);
Dept dept = new Dept();
try {
while (rs.next()) {
dept.setDid(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setParentid(rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
return dept;
}
public List<Dept> queryDemos(int id) {
String sql = "select * from dept where parentid=" + id;
ResultSet rs = baseDao.query(sql);
List<Dept> list = new ArrayList<Dept>();
try {
while (rs.next()) {
Dept dept = new Dept();
dept.setDid(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setParentid(rs.getInt(3));
list.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
IDemoDao dao = new DemoDaoImpl();
Dept d = dao.queryDemoById(1);
System.out.println(d);
}
}
package com.demo.serv;
import java.util.List;
import com.demo.bean.Dept;
import com.demo.dao.DemoDaoImpl;
import com.demo.dao.IDemoDao;
public class DemoServImpl implements IDemoServ {
private IDemoDao demoDao = new DemoDaoImpl();
private static String htmlStr="";
private void search(int id) {
List<Dept> list = demoDao.queryDemos(id);
htmlStr+="<ul>";
for (Dept dept : list) {
htmlStr+="<li>";
htmlStr+="<a href='"+dept.getDid()+"'>"+dept.getDname()+"</a>";
htmlStr+="</li>";
search(dept.getDid());
}
htmlStr+="</ul>";
}
public String resHTML(int id){
htmlStr="";
search(id);
return htmlStr;
}
}
package com.demo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.demo.serv.DemoServImpl;
import com.demo.serv.IDemoServ;
public class DemoServlet extends HttpServlet {
private IDemoServ demoserv = new DemoServImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int id = Integer.parseInt(request.getParameter("id"));
String reshtml = demoserv.resHTML(id);
out.write(reshtml);
out.flush();
out.close();
}
}