概况
mvc设计模式、mySQL的事务、serverlet
知识点
1.mvc设计模式
所谓mvc设计模式,就是将集中的代码放到不同的类的方法中,没个模块不必实现太多功能,如:一个聊天程序中,登录界面类只完成登录功能,网络连接类只完成网络连接的功能,聊天窗口类只写与聊天功能相关的方法,避免冗余。
2.事务
即一组不可拆分的操作。
代码1:
Connection conn = SQLManager.newInstance().getConnection();
try {
Statement state = conn.createStatement();
String sql1 = "insert into user(user_name,password) values('assa','245454')";
String sql2 = "insert into user(user_name,password) values('aa','2454574')";
String sql3 = "insert into user(user_name,passwords) values('asdssa','2574')";
String sql4 = "insert into user(user_name,password) values('asgggsa','2475454')";
String sql5 = "insert into user(user_name,password) values('assgssa','24665454')";
/*
* 事务:将与数据库的连接connection的事务自动提交设置为false
* 之后的语句一直到connection调用了commit()方法的位置,事务不自动提交,将五个独立的事务变成一个整体事务
* 一旦其中某条语句出错,全部不再执行
* 正常情况下,出错位置前面的语句会执行
*/
conn.setAutoCommit(false);
/*
*这五条语句是一个整体,是一个事务
*/
state.execute(sql1);
state.execute(sql2);
state.execute(sql3);
state.execute(sql4);
state.execute(sql5);
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
代码2
Connection conn = SQLManager.newInstance().getConnection();
try {
Statement state = conn.createStatement();
String sql1 = "insert into user(user_name,password) values('assa','245454')";
String sql2 = "insert into user(user_name,password) values('aa','2454574')";
String sql3 = "insert into user(user_name,passwords) values('asdssa','2574')";
String sql4 = "insert into user(user_name,password) values('asgggsa','2475454')";
String sql5 = "insert into user(user_name,password) values('assgssa','24665454')";
/*
* 相比于上一种,这种写法执行效率更高
*/
conn.setAutoCommit(false);
//将给定的 SQL 命令添加到此 Statement 对象的当前命令列表中。通过调用方法 executeBatch 可以批量执行此列表中的命令。
state.addBatch(sql1);
state.addBatch(sql2);
state.addBatch(sql3);
state.addBatch(sql4);
state.addBatch(sql5);
state.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
serverlet
概述:servelet的作用是给前端提供数据 。
代码演示:
首先建立一个serverlet类
/**
* Servlet implementation class MyServerlet
*/
@WebServlet("/MyServerlet")///MyServerlet在网页地址输入时会用到
public class MyServerlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServerlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.getWriter().append("Served at: ").append(request.getContextPath());
String userName = request.getParameter("userName");
String password = request.getParameter("password");
userName = Encoding.doEncoding(userName);
String s = "用户名:"+userName+" 密码"+password;
System.out.println(s);
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);
}
}
接着建立一个转换码的工具类
/*
*一般的网页地址都是ISO-8859-1的编码格式,此处需要将它转换为UTF-8格式
*/
public class Encoding {
public static String doEncoding(String string){
try {
byte[] array = string.getBytes("ISO-8859-1");
string = new String(array,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return string;
}
}
接着运行工程
输入网址,得到结果
运行结果