您的位置:首页 > 其它

day19

2015-08-11 19:52 246 查看

数据库的事件

java数据库中默认的是每读取一行代码就执行一次事件。但是有的时候我们会需要用到许多代码来组成一个事件,这里就要用到setAutoCommit(false);方法来取消默认的事件执行周期,然后再用commit()方法来把以上的代码当成一个事件来执行

String sql1 = "insert into user(user_name,password) values('asdasd','adasd')";
String sql2 = "insert into user(user_name,password) values('qwsdase','aderasd')";
String sql3 = "insert into user(user_name,password) values('ssdsd','adaasd')";
String sql4 = "insert into user(user_name,password) values('34322','adasxcd')";
String sql5 = "insert into user(user_name,password) values('asdwerasd','adazxcsd')";
Connection conn = SQLManager.Instance().getConn();
try {
Statement state1 = conn.createStatement();
conn.setAutoCommit(false);
state1.execute(sql1);
state1.execute(sql2);
state1.execute(sql3);
state1.execute(sql4);
state1.execute(sql5);
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}


运行结果:







说明:这里报错是证明了这五句话为一个事件,当其中出现错误时,五句话的结果都没有反映到数据库中,表明了是一个事件

Serverlet

import java.io.UnsupportedEncodingException;

public class Encoding {
public static String  doEncoding(String string ){
try {
byte[] arr  = string.getBytes("ISO-8859-1");
string = new String(arr,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return string;

}
}


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class ServerLetTest
*/
@WebServlet("/ServerLetTest")
public class ServerLetTest extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ServerLetTest() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
userName = Encoding.doEncoding(userName);
System.out.println("   "+userName+"  "+password);
String s = "用户名为:"+userName+",密码为:"+password;
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.getWriter().append(s);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}


运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: