首先用myeclipse创建一个web 工程
这是jsp登录页面
<%
request.setCharacterEncoding("GBK");
String name=request.getParameter("文件名");
if(name.equals("sa")){
session.setAttribute("UserName",name);
response.sendRedirect("文件名");
}
else{
response.sendRedirect("文件名");
}
%>
连接数据库文件
public class ConnectionManager {
private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;DatabaseName="数据库名“;
private static final String DATABASE_USRE = "sa";
private static final String DATABASE_PASSWORD = "sa";
/**
* 返回连接
*
* @return Connection
*/
public static Connection getConnection() {
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection = DriverManager.getConnection(DATABASE_URL,
DATABASE_USRE, DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
/**
* 关闭连接
*
* @param dbConnection
* Connection
*/
public static void closeConnection(Connection dbConnection) {
try {
if (dbConnection != null && (!dbConnection.isClosed())) {
dbConnection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
/**
* 关闭结果集
*/
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭语句
*/
public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}