java模板代码
来源:互联网 发布:淘宝卖家违规扣分 编辑:程序博客网 时间:2024/06/06 18:14
Window----Preferences----temp----java----Templates
//SQL查询模板代码(sql_s)[sql_select_template]
Connection conn = null;PreparedStatement st = null;
ResultSet rs = null;
try{
conn = JdbcUtils.getConnection();
String sql = "select * from 表名";${cursor}
st = conn.prepareStatement(sql);
rs = st.executeQuery();
while(rs.next()){
//do something
}
}catch (Exception e) {
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try{
conn = JdbcUtils.getConnection();
String sql = "";${cursor}
st = conn.prepareStatement(sql);
int num = st.executeUpdate();
if(num>0){
//do something
}
}catch (Exception e) {
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
/*JDBCIMPL模板代码!
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
conn=JdbcUtils.getConnection();
//先准备String sql,再通过连接准备PreparedStatement
String sql="?";${cursor}
st=conn.prepareStatement(sql);
st.setString(1, "");
int num=st.executeUpdate();
rs=st.executeQuery();
}catch (Exception e) {
throw new DaoException();
}finally{
JdbcUtils.release(conn, st, rs);
}
*/
//servlet的doPost方法try异常模板代码[try_post](servlet_post_try_catch_exception)
//谁能保证这儿不抛异常!
try{
//调用service处理业务!所示请求,都通过业务层实现!接口指向实现
BusinessService service = new BusinessServiceImpl();
${cursor}
//业务处理好之后,将message存到request域带给jsp显示!
request.setAttribute("message", "XX成功");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}catch (Exception e) {
//出现异常不要紧,记录下来,并给友好提示!
e.printStackTrace();
request.setAttribute("message", "XX失败");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
//EL函数tld模板
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<uri>/expressionlanguagefunction</uri>
<function>
<name>filter</name>
<function-class>cn.itcast.fn.HTMLFilter</function-class>
<function-signature>java.lang.String filter( java.lang.String )</function-signature>
</function>
<function>
<name>add</name>
<function-class>cn.itcast.fn.MyEL</function-class>
<function-signature>java.lang.String add( java.lang.String , java.lang.String)</function-signature>
</function>
</taglib>
//servlet的doPost方法try异常模板代码[try_post]
//谁能保证这儿不抛异常!
try{
//调用service处理业务!所示请求,都通过业务层实现!接口指向实现
BusinessService service = new BusinessServiceImpl();
${cursor}
//业务处理好之后,将message存到request域带给jsp显示!
request.setAttribute("message", "XX成功");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}catch (Exception e) {
//出现异常不要紧,记录下来,并给友好提示!
e.printStackTrace();
request.setAttribute("message", "XX失败");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
//字符流读取模板代码+IO异常处理机制[read_c](read_char_try_catch)
FileReader reader=null;
FileWriter writer=null;
try {
//reader=new FileReader(".\\1.txt");
//writer=new FileWriter("k:\\abc.txt");
//writer=new FileWriter("abc.txt");
//字符流必须用字符数组
char[] buf=new char[1024];
int len=0;
//一顿狂写
while((len=reader.read(buf))!=-1){
writer.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
}catch (IOException e) {
throw new RuntimeException("reader关闭失败!");
}
reader=null;
}
if(writer!=null){
try {
writer.close();
}catch (IOException e) {
throw new RuntimeException("writer关闭失败!");
}
writer=null;
}
}
//字节流读取模板代码+IO异常处理机制[read_b](read_byte_try_catch)
InputStream in=null;
OutputStream out=null;
try {
//in=new FileInputStream(".\\1.txt");//关联源
//out=new FileOutputStream("d:\\2.txt");//关联目的
//字节流必须用字节数组
byte[] buf=new byte[1024];
int len=0;
//一顿狂写
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
}catch (IOException e) {
throw new RuntimeException("in关闭失败!");
}
in=null;
}
if(out!=null){
try {
out.close();
}catch (IOException e) {
throw new RuntimeException("out关闭失败!");
}
out=null;
}
}
数据库创建库和表模板代码
mysql -uroot -proot
set character_set_client=gb2312;
set character_set_results=gb2312;
create database day15 character set utf8 collate utf8_general_ci;
use day15;
create table testtext(
id varchar(40) primary key,
resume text
);
show tables;
select * from testtext;
//实现批处理第一种方式:Statement对象!
//SQL实现批处理第1种方式模板代码(sql_batch_st)[sql_batch_statement]
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql1 = "";
String sql2 = "";
st = conn.createStatement();
//Statement对象批量插入标准3步曲!
st.addBatch(sql1);
st.addBatch(sql2);
//返回Statement对象内部的list集合每一条命令影响的行数组成的数组!
int[] num=st.executeBatch();
st.clearBatch();
} catch (Exception e) {
throw new DaoException(e);
} finally {
JdbcUtils.release(conn, st, rs);
}
//实现批处理第2种方式:PreparedStatement对象!
//SQL实现批处理第2种方式模板代码(sql_batch_prest)[sql_batch_preparedstatement]
//记录开始时间
long startTime = System.currentTimeMillis();
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "?,?";
st = conn.prepareStatement(sql);
//PreparedStatement对象批量插入标准3步曲!
for (int i = 1; i < 1006; i++) {
//第1步,占位符全替换
st.setString(1, i+"");
st.setString(2,i+"");
//第2步,将完整的sql语句添加到内部list
st.addBatch();
//第3步,健壮性判断,防止java heap space溢出
if (i%100==0) {
//达到一定数目,就先执行,并清空list里的Sql
st.executeBatch();
st.clearBatch();
}
//第4步,最后将无法整除的漏网之鱼执行
st.executeBatch();
st.clearBatch();
}
} catch (Exception e) {
throw new DaoException(e);
} finally {
JdbcUtils.release(conn, st, rs);
}
//记录结束时间!
long endTime = System.currentTimeMillis();
System.out.println("总花了:" + (endTime-startTime)/1000 + "秒");
0 0
- java代码格式化模板
- JAVA代码注释模板
- Java代码注释模板
- java模板代码
- Java 测试代码模板
- JAVA代码注释模板
- Java图片压缩模板代码
- 【Java】Eclipse 自定义代码模板
- java excel导出模板代码
- java模板引擎替换代码
- 模板设置(java代码)
- myeclipse优化+自定义java注释模板+自定义Java代码模板
- Eclipse Java代码注释模板的设置
- Eclipse Java代码注释模板的设置
- MyEclipse设置Java代码注释模板
- eclipse中添加Java代码注释模板
- myeclipse中java代码注释模板问题
- 服务器通过模板发送邮件java代码
- MAC上搭建Windows Phone 8 开发环境——VMware Fusion下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法
- Mac OS下文件夹(带linux部分)
- 黑马程序员之7k面试题之银行业务调度系统
- setup time & hold time
- LeetCode 之 Letter Combinations of a Phone Number
- java模板代码
- C# 事件委托 【开门两次 开枪】
- MQTT的学习研究(九)基于HTTP GET MQTT 抓取消息服务端使用
- “corruption in the heap” vector内存释放错误
- UIAlertView笔记
- C++标准库类型(转)
- 算法导论第15章 最优二叉树
- javascript获取光标位置以及设置光标位置(兼容firefox)
- 迭代器是什么