java dbhelper通用类,DbHelper数据库通用类使用方法

代码

//

执行SQL语句

public

static

void

ExecSqlCommand()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

select * from t1_insert

"

);

db.ExecuteNonQuery(command);

}

//

执行存储过程

public

static

void

ExecStoredProcedure()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t1_insert

"

);

db.AddInParameter(command,

"

@id

"

, DbType.String,

"

heihei

"

);

db.AddInParameter(command,

"

@id

"

, DbType.String,

"

heihei

"

);

db.ExecuteNonQuery(command);

}

//

返回DataTable

public

static

void

GetDataTable()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

t1_findall

"

);

DataTable dt

=

db.ExecuteDataTable(command);

}

//

获取各种返回值

public

static

void

GetAllParameter()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(command,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(command,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(command,

"

@returnValue

"

, DbType.Int32);

db.ExecuteNonQuery(command);

string

s = db.GetParameter(command,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(command,

"

@returnValue

"

).Value);

//

return value

}

//

DateReader操作

public

static

void

GetDataReader()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(command,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(command,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(command,

"

@returnValue

"

, DbType.Int32);

using

(DbDataReader reader =

db.ExecuteReader(command))

{

//

dt.Load(reader);

}

string

s = db.GetParameter(command,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(command,

"

@returnValue

"

).Value);

//

return value

}

//

获取DataSet

public

static

void

GetDataSet()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

select * from t1

"

);

DataSet ds

=

db.ExecuteDataSet(command);

}

//

事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)

public

void

DoBusiness()

{

using

(Trans t =

new

Trans())

{

try

{

D1(t);

throw

new

Exception();

//

如果有异常,会回滚滴

D2(t);

t.Commit();

}

catch

{

t.RollBack();

}

}

}

public

void

D1(Trans t)

{

DbHelper db

=

new

DbHelper();

DbCommand cmd

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(cmd,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(cmd,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(cmd,

"

@returnValue

"

, DbType.Int32);

if

(t ==

null

)

db.ExecuteNonQuery(cmd);

else

db.ExecuteNonQuery(cmd,t);

string

s = db.GetParameter(cmd,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(cmd,

"

@returnValue

"

).Value);

//

return value

}

public

void

D2(Trans t)

{

DbHelper db

=

new

DbHelper();

DbCommand cmd

= db.GetSqlStringCommond(

"

insert t1 (id)values(‘..‘)

"

);

if

(t ==

null

)

db.ExecuteNonQuery(cmd);

else

db.ExecuteNonQuery(cmd, t);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 JDBC 的 DBHelper 辅助类的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBHelper { private static final String DRIVER = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/database_name"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; private Connection conn; private PreparedStatement ps; private ResultSet rs; public DBHelper() { try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet executeQuery(String sql, Object... params) throws SQLException { ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } rs = ps.executeQuery(); return rs; } public int executeUpdate(String sql, Object... params) throws SQLException { ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } return ps.executeUpdate(); } public void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 这个 DBHelper 类包含了连接数据库、执行查询和更新操作、关闭数据库连接等常用方法使用时,只需实例化 DBHelper 类,然后调用其中的方法即可。例如: ```java DBHelper dbHelper = new DBHelper(); try { ResultSet rs = dbHelper.executeQuery("SELECT * FROM table_name WHERE column_name = ?", "value"); while (rs.next()) { // 处理查询结果 } } catch (SQLException e) { e.printStackTrace(); } finally { dbHelper.close(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值