日期:2014-05-18  浏览次数:20624 次

server端连接database总有问题
MyEclipse+MySQL+Tomcat

想建立一个ConnectionManager用来连接database,可以用于每个功能
database中有一个table:account(username,password,tele,email)

ConnectionManager.java如下

package com.lu.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionManager {
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/grocery?useUnicode=true&characterEncoding=utf8";
private static final String DATABASE_USERNAME = "root";
private static final String DATEBASE_PASSWORD = "0000";

// 获取连接
public static Connection getConnection() throws SQLException {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = DriverManager.getConnection(DATABASE_URL,
DATABASE_USERNAME, DATEBASE_PASSWORD);
return con;
}

public static void main(String[] args) {
try {
Connection con=getConnection();
Statement stmt=con.createStatement();
stmt.execute("select account(username,password) values('wangwu','123')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


LoginServlet.java如下:
package com.lu.servlet;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lu.db.ConnectionManager;
import com.lu.model.User;

@SuppressWarnings("unused")
public class LoginServlet extends HttpServlet {
/**

*/
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html;charset=gb2312");
// 取出表单中用户输入的用户名
String username = request.getParameter("username");
System.out.println("=================>"+username);
// 取出表单中用户输入的密码
System.out.println("u能传过来吗?"+username);
String password = request.getParameter("password");
System.out.println("p能传过来吗?"+password);
PrintWriter pw=resp.getWriter();
// OutputStream out=resp.getOutputStream();
// 创建出用户
User user = new User(username, password);
if (isExist(user)) {
// 为真 说明用户存在
request.setAttribute("username", username);
resp.sendRedirect("success.jsp");
// req.setAttribute("lab", "true");

pw.println(true);
// out.write(1);
} else {
resp.sendRedirect("error.jsp");
pw.println(false);
// out.write(2);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw=resp.getWriter();
String username = req.getParameter("username");
System.out.println("u能传过来吗?"+username);
String password = req.getParameter("password");
System.out.println("