hibernate执行原生Sql语句的方法(转)
http://www.thecloud.cn/study/program/java/597.html
hibernate3的HQL 是面向对象的语法,已经支持 update ,delete from语句,但
目前还不支持insert into语句.
做项目有时候的确需要写原生的sql来方便web开发.
下面是我自己鼓捣出来的方法,希望对我和朋友有所帮助.
在Dao里写一个方法,让我们在action里调用它...
public void InsertData(String MytblName,String
Mytitle,String Mycode,String Mycoments){
log.debug("insert into dataitems ---drs");
//使用了原生sql语句
try{
//将传入的类名处理成表名 例如:CTable----〉C_Table
MytblName = "C_"+MytblName.substring(1,MytblName.length());
//插入表 的
sql
String Insert_SQL ="INSERT INTO "+MytblName+
"
(C_Title,C_Code,C_Comments)"+
" VALUES
('"+Mytitle+"','"+Mycode+"','"+Mycoments+"')" ;
//获得当前session的数据库连接
Connection
CurConn =
getSession().connection();
//System.out.println(Insert_SQL);
//生成一个Statement对象
PreparedStatement ps = CurConn.prepareStatement(Insert_SQL);
//执行查询
ps.execute();
//关闭该对象
ps.close();
getSession().flush();
log.debug("insert into dataitems
successful");
}
catch
(SQLException Re ){ // 异常
log.error("insert into dataitems failed", Re);
Re.printStackTrace();
}
}
相关文档:
(1)表名:购物信息
购物人 商品名称 数量
A 甲 2
B 乙  ......
今天安装SQL SERVER 2000 个人版,安装最后弹出错误对话框:
“安装程序配置服务器失败。参考服务器错误日志和C:\windows\sqlstp.log”,从而无法安装。
到具体的目录查看显示"一般性网络错误",在网上搜索问题原因及解决方案如下:
此问题属于sql server的bug, GetComputerName 用于获取本地计算机名。 ......
ORDER BY 子句按一列或多列(最多 8,060 个字节)对查询结果进行排序。有关 ORDER BY 子句最大大小的详细信息,请参阅 ORDER BY 子句 (Transact-SQL)。
Microsoft SQL Server 2005 允许在 from 子句中指定对 SELECT 列表中未指定的表中的列进行排序。ORDE ......
在SQL Server的性能调优中,有一个不可比拟的问题:那就是如何在一段需要长时间的代码或被频繁调用的代码中处理临时数据集?表变量和临时表是两种选择。
在SQL Server的性能调优中,有一个不可比拟的问题:那就是如何在一段需要长时间的代码或被频繁调用的代码中处理临时数据集?表变量和临时表是两种选择。记得在给一家 ......
用EXPLAIN PLAN 分析SQL语句
http://blog.csdn.net/kj021320/archive/2006/08/19/1096021.aspx
如何生成explain plan?
解答:运行utlxplan.sql. 建立plan 表
针对特定SQL语句,使用 explain plan set statement_id = 'tst1' into plan_table
运行utlxplp.sql 或 utlxpls.sql察看explain plan
EXPLAIN PLA ......