Skip to content

Commit

Permalink
Merge pull request #516 from Danivy/master
Browse files Browse the repository at this point in the history
Homework
  • Loading branch information
zengsn authored Nov 21, 2016
2 parents 7019a41 + 1cc3eff commit 7d64fbb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 25 deletions.
74 changes: 74 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902205/DBBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package edu.hzu.javaweb.labs.se1414080902205;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import java.sql.*;

public class DBBean {
private Connection conn = null;
private PreparedStatement PPstmt = null;
private Statement stmt = null;
//连接mysql数据库中的test数据库
public Connection getConnection(){
try{
if(conn == null){
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/youju?useUnicode=true&charset=gbk","root","root");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("未能加载数据库驱动程序");
}catch(SQLException e0){
e0.printStackTrace();
System.out.println("数据库连接失败");
}
return conn;
}

//创建语句对象
public PreparedStatement getPreparedStatement(String sql){
try{
conn = getConnection();
PPstmt = conn.prepareStatement(sql);
}catch(SQLException e){
e.printStackTrace();
System.out.println("数据库预备声明创建失败");
}

return PPstmt;
}
public Statement getStatement(){
try{
stmt = conn.createStatement();
}catch(SQLException e){
System.out.println(e);
e.printStackTrace();
System.out.println("数据库声明创建失败");
}
return stmt;
}
public void closeResource(Connection conn,Statement stmt,PreparedStatement PPstmt){
try{

if(conn != null)
conn.close();
if(stmt != null)
stmt.close();
if(PPstmt != null){
PPstmt.close();
}
}catch(SQLException e0){
System.out.println("数据库资源关闭失败");
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/1414080902205")
public class Se1414080902205Servlet extends HttpServlet {

Expand Down Expand Up @@ -54,30 +56,21 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
String Account = "";
String Amount ="";
if(request.getParameter("username")!=null){
// System.out.println("fkldasjflkasjdkflas");
Account+=new String(request.getParameter("username"));
}

if(request.getParameter("passwd")!=null)
Amount= new String(request.getParameter("passwd"));
String Account = new String(request.getParameter("account"));
String Amount = new String(request.getParameter("amount"));
// out.print(Account);
// out.print(Amount);
HttpSession session=request.getSession(true);
String msg = "";
if (Account.equals("")) {
msg = "账号不能为空";
out.println("账号不能为空");
} else if (Amount.equals("")) {
msg = "转账金额不能为空";
out.println("转账金额不能为空");
} else {
boolean flat = true;
char[] str=Account.toCharArray();
for (int i = 0;i < Account.length();i++) {
int x = str[i] - '0';
if (!(x >= 0 && x <= 9)) {
msg = "请输入正确的账号";
out.println("请输入正确的账号");
flat = false;
break;
}
Expand All @@ -87,22 +80,30 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
for (int i = 0;i < Amount.length();i++) {
int x = sstr[i] - '0';
if (!(x >= 0 && x <= 9)) {
msg = "请输入正确的转账金额";
out.println("请输入正确的转账金额");
flat = false;
break;
}
}
}
if (flat) msg = "转账成功";
if (flat) {
Connection conn = null;
PreparedStatement stmt = null;
DBBean db = new DBBean();
try {
conn = db.getConnection();
String sql = "insert into transfer values(?,?)";
stmt = db.getPreparedStatement(sql);
stmt.setString(1,Account);
stmt.setString(2,Amount);
stmt.executeUpdate();
db.closeResource(conn,null,stmt);
} catch (SQLException e) {
e.printStackTrace();
}
out.println("转账成功");
}
}
System.out.println(msg);
request.setAttribute("message", msg);
request.getRequestDispatcher("/index.jsp").forward(request, response);
// System.out.println("dflajskldfjads");
session.setAttribute("message", msg);
// System.out.println(session.getAttribute("message").toString());
//System.out.println("fsfsdfsd");
// request.getSession().setAttribute("message",msg);
// String Account = "1414080902205";
// String Amount = "1414080902205";
// out.println(Account);
Expand Down

0 comments on commit 7d64fbb

Please sign in to comment.