1.使用commons DButils組件對數據庫進行查詢。
首先使用數據源以及本組件進行數據庫的鏈接,建立QueryRunner類型的對象。
使用數組,對sql語句中的佔位符進行填充。
建立MapListHandler類型對象,用於裝載每一條記錄。
全部的記錄都以List形式返回,使用代碼中所示結果,對記錄進行讀取。
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- out.print("Test AND I am so BORING!<br>");
- request.setCharacterEncoding("utf-8");
- Context context;
- try {
- context = new InitialContext();
- DataSource ds=(DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
- QueryRunner qr=new QueryRunner(ds);
- // String sql="select * from users";
- String sql="select * from users where id=? and name=?";
-
- MapListHandler handler=new MapListHandler();
- String [] param={"2","meng"};
- // List list=(List)qr.query(sql,handler);
- List list=(List)qr.query(sql,param,handler);
-
- for(int i=0;i<list.size();i++){
- Map map=(Map)list.get(i);
- out.println(map.get("id"));
- out.println(map.get("name"));
- out.print("<br>");
- }
-
- } catch (NamingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- out.flush();
- out.close();
- }
-
- }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("Test AND I am so BORING!<br>");
request.setCharacterEncoding("utf-8");
Context context;
try {
context = new InitialContext();
DataSource ds=(DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
QueryRunner qr=new QueryRunner(ds);
// String sql="select * from users";
String sql="select * from users where id=? and name=?";
MapListHandler handler=new MapListHandler();
String [] param={"2","meng"};
// List list=(List)qr.query(sql,handler);
List list=(List)qr.query(sql,param,handler);
for(int i=0;i<list.size();i++){
Map map=(Map)list.get(i);
out.println(map.get("id"));
out.println(map.get("name"));
out.print("<br>");
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
out.close();
}
}
2.使用本組件對數據庫進行數據管理。包括「增刪改」。
最開始同樣,對數據庫進行鏈接,而後建立QueryRunner類型的對象,因爲對數據的管理不返回一個結果集,因此只返回一個int類型的變量。
同樣使用數組對sql語句中的佔位符進行填充。
最後根據返回的值的大小(和0比),來肯定是否成功執行本語句。
- public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
-
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- out.println("Test Again!");
- String sql="insert into users values(?,?) ";
- Context context;
- try {
- context = new InitialContext();
-
- DataSource ds=(DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
- QueryRunner qr=new QueryRunner(ds);
- // MapListHandler handler=new MapListHandler();
- String [] params={"3","Zhang"};
- int i=qr.update(sql, params);
- if(i>0)
- {
- out.println("成功!");
- }
- else
- {
- out.println("Failed!");
- }
- } catch (NamingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("Test Again!");
String sql="insert into users values(?,?) ";
Context context;
try {
context = new InitialContext();
DataSource ds=(DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
QueryRunner qr=new QueryRunner(ds);
// MapListHandler handler=new MapListHandler();
String [] params={"3","Zhang"};
int i=qr.update(sql, params);
if(i>0)
{
out.println("成功!");
}
else
{
out.println("Failed!");
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
別外加:
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.DriverManager;
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.dbutils.ResultSetHandler;
- import org.apache.commons.dbutils.handlers.BeanListHandler;
-
- public class dbtest
- {
-
- infoBean bean = new infoBean();
-
- BeanListHandler h = new BeanListHandler(bean.getClass());
- Connection connect = null;
- List result = new ArrayList();
-
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- connect = DriverManager.getConnection(
- "jdbc:mysql://localhost/normaltest", "root", "root");
-
-
- QueryRunner run = new QueryRunner();
-
- result = (ArrayList) run.query(connect,
- "SELECT username,age,money,usertime FROM info", h);
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- return result;
- }