日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c mysql 工具类_Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类

發布時間:2025/3/15 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c mysql 工具类_Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該工具類是在JavaWeb中連接mysql所用到的通用工具類

該類用于Java+Servlet的編程中,方便數據庫的操作,連接,獲取其列表值。下面是這個數據庫操作類的通用方法,基本上能夠用于類里面只含有簡單數據的類,例如類是Date,int,double,String等數據庫里面包含的類型。

這個并不是一個模板,而是一個工具類,也就是說,符合只有簡單數據的類可以直接調用以下提供的功能就能操作數據庫,返回類的List的值。

之前在進行編程的時候發現在操作數據庫的過程中特別麻煩,每次重新寫一個新類要存儲到數據庫的話,總是要在service寫新的方法才能適應相應的操作,但是我覺得這些方法大都大同小異,似乎沒有必要每次都為一個類寫一個方法適應一個新的類。所以寫這個通用數據庫操作的類的想法就產生了。

首先,該工具主要是對數據庫進行操作,所以首先要對數據庫進行連接,下面是實現對數據庫連接的一個DBUtil.java文件,實現了對數據庫的連接

packagecom.hjf.util;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/*** 數據庫連接工具

*@authorHDQ

**/

public classDBUtil {public static String db_url = "jdbc:mysql://localhost:3306/數據庫名";public static String db_user = "數據庫用戶名";public static String db_pass = "數據庫密碼";public staticConnection getConn () {

Connection conn= null;try{

Class.forName("com.mysql.jdbc.Driver");//加載驅動

conn =DriverManager.getConnection(db_url, db_user, db_pass);

}catch(Exception e) {

e.printStackTrace();

}returnconn;

}/*** 關閉連接

*@paramstate

*@paramconn*/

public static voidclose (Statement state, Connection conn) {if (state != null) {try{

state.close();

}catch(SQLException e) {

e.printStackTrace();

}

}if (conn != null) {try{

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}

}

}public static voidclose (ResultSet rs, Statement state, Connection conn) {if (rs != null) {try{

rs.close();

}catch(SQLException e) {

e.printStackTrace();

}

}if (state != null) {try{

state.close();

}catch(SQLException e) {

e.printStackTrace();

}

}if (conn != null) {try{

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}

}

}public static void main(String[] args) throwsSQLException {

Connection conn=getConn();

PreparedStatement pstmt= null;

ResultSet rs= null;

String sql="select * from course";

pstmt=conn.prepareStatement(sql);

rs=pstmt.executeQuery();if(rs.next()){

System.out.println("空");

}else{

System.out.println("不空");

}

}

}

然后在對數據庫連接完成之后,接著就是對數據庫進行操作的一個類了,這個類位于dao層

這個ClassDao.java類可以根據傳遞進來的參數返回相應類型的類的List,其實現的原理是利用Field 反射其值到對應的類里面,所以在使用的時候帶有返回List的功能的時候,其數據庫里面的數據項名稱和類里面定義的變量的名稱需要保持一致,才能保證其值被正確反射到類里面,反射機制所以返回的類的List都已經被賦予相應的值,可以直接被使用

相應的操作方法用法會在本文的最后給出,ClassDao.java是一個通用操作類,實現了對數據庫操作

packagecom.hjf.dao;importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.hjf.util.DBUtil;/*** 通用類Dao

* Dao層操作數據

*@authorHDQ

**/

public classClassDao {/*** 添加*@return

*/

public booleanadd(String table,String []strList,String []strList1) {if(strList.length==0)return false;

String sql= "insert into "+table+"(";for(int i=0;i

{if(i!=strList.length-1)

sql+=strList[i]+",";else sql+=strList[i]+")";

}

sql+=" values('";for(int i=0;i

{if(i!=strList1.length-1)

sql+=strList1[i]+"','";else sql+=strList1[i]+"')";

}//創建數據庫鏈接

Connection conn =DBUtil.getConn();

Statement state= null;boolean f = false;int a = 0;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally{//關閉連接

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 刪除

**@return

*/

public booleandelete (String table,String zhixing,String biaoshi) {boolean f = false;

String sql= "delete from "+table+" where "+zhixing+"='" + biaoshi + "'";

Connection conn=DBUtil.getConn();

Statement state= null;int a = 0;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 修改*/

public booleanupdate(String table,String []strlist,String []strlist1,String qian,String hou) {

String sql= "update "+table+" set ";for(int i=0;i

{if(i!=strlist.length-1)

sql+=strlist[i]+"='" + strlist1[i] + "',";else sql+=strlist[i]+"='" + strlist1[i] + "' where "+qian+"='" + hou + "'";

}

Connection conn=DBUtil.getConn();

Statement state= null;boolean f = false;int a = 0;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 驗證通用類名稱是否唯一

* true --- 不唯一

*@return

*/

public booleanname(String table,String zhi,String weiyi) {boolean flag = false;

String sql= "select "+zhi+" from "+table+" where "+zhi+" = '" + weiyi + "'";

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);while(rs.next()) {

flag= true;

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returnflag;

}/*** 查找*@return*@throwsIllegalAccessException

*@throwsInstantiationException*/@SuppressWarnings("deprecation")public List search(String table,String []strList,String []strList1,Class clazz) throwsInstantiationException, IllegalAccessException {

String sql= "select * from "+table;int i=0,k=0;for(String it:strList1)

{if(it!=null&&!it.equals(""))

{if(k==0)

sql+=" where "+ strList[i]+" like '%" + it + "%'";else sql +=" and "+ strList[i]+" like '%" + it + "%'";++k;

}++i;

}

List list = new ArrayList<>();

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);

T bean= null;while(rs.next()) {

bean=clazz.newInstance();for(String it:strList)

{

Field fs=getDeclaredField(bean, it);if(fs==null){throw new IllegalArgumentException("Could not find field["+it+"] on target ["+bean+"]");

}

makeAccessiable(fs);try{

fs.set(bean, rs.getObject(it));

}catch(IllegalAccessException e){

System.out.println("不可能拋出的異常");

}

}

list.add(bean);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returnlist;

}/*** 由時間和條件查找*@return*@throwsIllegalAccessException

*@throwsInstantiationException*/@SuppressWarnings("deprecation")public List searchByTime(String table,String []strList,String []strList1,String biaoshi,String qian,String hou,Class clazz) throwsInstantiationException, IllegalAccessException {

String sql= "select * from "+table+" where ";int i=0,k=0;for(String it:strList1)

{if(it!=null&&!it.equals(""))

{

sql+= strList[i]+" like '%" + it + "%'";++k;

}++i;

}if(qian!=null&&!qian.equals(""))

{if(k>0)

sql+=" and "+biaoshi+" Between '"+qian+"' AND '"+hou+"'";else sql+=biaoshi+" Between '"+qian+"' AND '"+hou+"'";

}//and shijian Between '"+request.getParameter("shijian1")+"' AND '"+request.getParameter("shijian2")+"'"//查詢的時間格式例如:2015-10-27 24:00:0(假如為DateTime的話,Date只需要年-月-日)

List list = new ArrayList<>();

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);

T bean= null;while(rs.next()) {

bean=clazz.newInstance();for(String it:strList)

{

Field fs=getDeclaredField(bean, it);if(fs==null){throw new IllegalArgumentException("Could not find field["+it+"] on target ["+bean+"]");

}

makeAccessiable(fs);try{

fs.set(bean, rs.getObject(it));

}catch(IllegalAccessException e){

System.out.println("不可能拋出的異常");

}

}

list.add(bean);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returnlist;

}/*** 創建數據庫*@return*@throwsClassNotFoundException

*@throwsIllegalAccessException

*@throwsInstantiationException*/

public boolean createTable(String table,String []info,String []type,int[]size)

{

String sql= "CREATE TABLE "+table+"(";

String lei[]=new String[] {"char","varchar"};int i=0;for(String it:info)

{if(!it.equals(""))

{boolean g_trit=false;for(String sit:lei)

{if(type[i].toLowerCase().contains(sit.toLowerCase()))

{

g_trit=true;

}

}if(g_trit)

sql+= it+" "+type[i]+"("+size[i]+")";else sql += it+" "+type[i];

}if(i!=info.length-1)

sql+=",";++i;

}

sql+=")";//and shijian Between '"+request.getParameter("shijian1")+"' AND '"+request.getParameter("shijian2")+"'"//查詢的時間格式例如:2015-10-27 24:00:0

Connection conn =DBUtil.getConn();

Statement state= null;

ResultSet rs= null;int a=0;boolean f=false;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}if(a>0)

f=true;returnf;

}/*** 全部數據*@return*@throwsClassNotFoundException

*@throwsIllegalAccessException

*@throwsInstantiationException*/@SuppressWarnings("deprecation")public List list(String table,String []strList,Class clazz) throwsClassNotFoundException, InstantiationException, IllegalAccessException {

String sql= "select * from "+table;

List list = new ArrayList<>();

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);

T bean= null;while(rs.next()) {

bean=clazz.newInstance();for(String it:strList)

{

Field fs=getDeclaredField(bean, it);if(fs==null){throw new IllegalArgumentException("Could not find field["+it+"] on target ["+bean+"]");

}

makeAccessiable(fs);try{

fs.set(bean, rs.getObject(it));

}catch(IllegalAccessException e){

System.out.println("不可能拋出的異常");

}

}

list.add(bean);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returnlist;

}//獲取field屬性,屬性有可能在父類中繼承

public staticField getDeclaredField(Object obj,String fieldName){for (Class> clazz=obj.getClass(); clazz!=Object.class; clazz=clazz.getSuperclass()){try{returnclazz.getDeclaredField(fieldName);

}catch(Exception e){

}

}return null;

}//判斷field的修飾符是否是public,并據此改變field的訪問權限

public static voidmakeAccessiable(Field field){if(!Modifier.isPublic(field.getModifiers())){

field.setAccessible(true);

}

}

}

在dao層里面的方法應該是為了安全性考慮吧,還要有一個sevice類來調用dao層里面的方法,就相當一個中介一樣吧,也就是一個提供dao層里面接口的一個類,以下是位于service層的service.java,實現dao層方法的接口

packagecom.hjf.service;importjava.util.List;importcom.hjf.dao.ClassDao;/*** CourseService

* 服務層

*@authorHDQ

**/

public classClassService {

ClassDao cDao= newClassDao();/*** 添加

*@paramcourse

*@return

*/

public booleanadd(String table,String strList[],String strList1[]) {boolean f =cDao.add(table,strList,strList1);returnf;

}/*** 刪除*/

public booleandel(String table,String qian,String hou) {returncDao.delete(table,qian,hou);

}/*** 修改

*@return

*/

public booleanupdate(String table,String []strlist,String []strlist1,String qian,String hou) {returncDao.update(table,strlist,strlist1,qian,hou);

}/*** 查找

*@return*@throwsIllegalAccessException

*@throwsInstantiationException*/

public List search(String table, String []strList, String []strList1,Class clazz) throwsInstantiationException, IllegalAccessException {returncDao.search(table,strList,strList1,clazz);

}/*** 由時間查找

*@return*@throwsIllegalAccessException

*@throwsInstantiationException*/

public List searchByTime(String table, String []strList, String []strList1,String biaoshi,String qian,String hou,Class clazz) throwsInstantiationException, IllegalAccessException {returncDao.searchByTime(table, strList, strList1, biaoshi, qian, hou, clazz);

}/*** 全部數據

*@return*@throwsIllegalAccessException

*@throwsInstantiationException

*@throwsClassNotFoundException*/

public List list(String table,String []strList,Class clazz) throwsClassNotFoundException, InstantiationException, IllegalAccessException {returncDao.list(table,strList,clazz);

}/*** 創建數據庫表單

*@return*@throwsIllegalAccessException

*@throwsInstantiationException*/

public boolean createTable(String table,String []info,String []type,int[]size)

{returncDao.createTable(table, info, type, size);

}

}

其3個類組成的工具類就如上面所示。

就以一個查找功能為例子,我現在Entity層中有3個類用到了這個通用類,假如現在想再定義一個新類調用這個通用數據庫的函數,實現對數據庫對這個類的數據的增刪改查功能,以下就以一個具體的例子來展示如何調用這個通用類的函數

然后下面介紹這個通用類的用法:

首先假如現在有一個類,位于entity層,假如是一個老師類,Teacher

然后在entity層中定義一個Teacher類,其中含有int類型的編號id,String類型的名字name,還有Date類型的出生日期birth,(定義的類的多少不受限制),并定義getter和setter的方法

定義完這個類之后,數據庫里面對應的項的名稱要和類的名稱一致,這樣才能被正確反射

所以在數據庫里面新建一個teacherlist的表,對應的項的名稱設置為int類型的id,char或者text類型的name,Date類型的birth

名字分別都與Teacher類里面的名稱相對應

雖然該工具類里面有提供表創建的函數,但是為了方便讀者理解,這里用數據庫管理軟件的創建過程來展示

創建完成之后,要實現往這個數據庫的teacherlist里面添加數據的功能的話,我們查看一下service層提供的接口

這個是ClassServlet里面的前綴,ClassService就是數據庫通用工具的接口類,可以通過調用這個service里面的函數實現數據的操作

//--------------添加數據功能--------------

提供的方法是add(table,strList[],strList1[]),返回的類型是boolean類型,表示成功與否

第一個參數是table表名,第二個參數strList是需要添加的項的名稱字符串組,第三個參數strList1是對應要需要添加項的內容

例如執行

String []strList=new String[] {"id","name","birth"};

String []strList1=new String[] {"11","王老師",“2001-09-22”};

service.add("teacherlist",strList,strList1);

就會將數據庫中teacherlist表里面對應的id,name,birth項添加上11,王老師,2001-09-22的信息

值得注意的是,strLis[]里面的字符串要對應和類中的變量名稱相等,而且數據庫中的名稱也是,這樣才能正確的反射,當然,添加的過程并沒有用到反射,但是規范化而言,用到list等功能的時候就可以直接使用了。

當出現數據添加失敗的時候,add函數會返回一個false,執行成功時則是true

//--------------獲取列表信息功能--------------

首先上述是teacherlist數據庫表的信息,現在如果我們想把所有信息獲取到一個List里面,可以調用service提供的

獲取全部數據的方法

list(table,strlist[],Class);

其中參數table是要獲取數據的表格名稱,strList[]填寫的是要獲取數據庫中的哪個項的名稱所對應的信息,Class是需要返回的List<>數組的類型,不過這個返回的類型一般還得強制轉換一下

以獲取上述的數據到一個List里面為例子,調用的函數如下所示:

String []strList=new String[]{"id","name","birth"};

@SuppressWarnings("unchecked")

List teachers= (List) service.list("teacherlist",strList,new Teacher().getClass());

此時teachers變量里面就儲存了你想要的數據了。然后,如果想要獲取的數據項沒有birth這個的話,在strList中填寫對應想要獲取的數據項就可以了,String []strList=new String[]{"id","name"};再執行獲取的函數,這個時候獲取的teachers里面的birth就沒有儲存數據了

然后這里拓展一點小知識,獲取的list可以存儲到attribute里面后直接在網頁上輸出,當然要利用到jstl.jar和standard.jar這兩個包

這里調用tlist這個函數的時候,將會跳轉到teacherlist這個網頁

Insert title here

Objectmessage=request.getAttribute("message");if(message!=null &&!"".equals(message)){%>

alert("");

老師信息列表

返回主頁

編號id名字出生日期${item.id}${item.name}${item.birth}

總結

以上是生活随笔為你收集整理的c mysql 工具类_Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。