标签:
src目录下建立mysql.properties文件
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/db_v?useUnicode=true&characterEncoding=utf8
mysql.username=root
mysql.password=a
建立DBUtil.java文件
package com.b2c.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
private static String driver = "" ;
private static String url = "" ;
private static String username = "" ;
private static String password = "" ;
static {
Properties pp = new Properties();
try {
pp.load(DBUtil.class.getClassLoader().getResourceAsStream("mysql.properties"));
driver = pp.getProperty("mysql.driver");
url = pp.getProperty("mysql.url");
username = pp.getProperty("mysql.username");
password = pp.getProperty("mysql.password");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection (){
Connection conn = null ;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn ;
}
public static void close(ResultSet rs , PreparedStatement ps , Connection conn){
try {
if(rs!=null /*&& !rs.isClosed()*/){
rs.close();
}
if(ps!=null/* && !ps.isClosed()*/){
ps.close();
}
if(conn!=null /*&& !conn.isClosed()*/){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(DBUtil.getConnection());
}
}
运行该文件可看到数据库已连接
示例
SubmitRegisterSetPasswordAction.java文件
package com.b2c.user.action;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.b2c.entity.User;
import com.b2c.utils.DBUtil;
import com.b2c.utils.EncrtypeUtil;
public class SubmitRegisterSetPasswordAction extends HttpServlet {
private static final long serialVersionUID = 8064991878705626238L;
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user_password = request.getParameter("user_password");
String user_phone = (String)request.getSession().getAttribute("user_phone");
String sql = "insert into t_v_user (user_phone , user_password , user_register_time , user_last_login_time ) values (?,?,?,?) " ;
Connection conn = null ;
PreparedStatement ps = null ;
User user = new User();
conn = DBUtil.getConnection();
String temp_time = System.currentTimeMillis() + "" ;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, user_phone);
ps.setString(2, EncrtypeUtil.getEncryptStr(user_password, "sha"));
ps.setString(3, temp_time);
ps.setString(4, temp_time);
ps.executeUpdate();//返回值是本次执行影响到数据表中的记录条数
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(null, ps, conn);
}
user.setUser_phone(user_phone);
user.setUser_register_time(temp_time);
user.setUser_last_login_time(temp_time);
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("/WEB-INF/user/user_register_set_point_page.jsp").forward(request, response);
return ;
}
}
标签:
原文地址:http://www.cnblogs.com/ganjun/p/5047543.html