怎样用 myeclipse和数据库做一个简单的登录界面,用来完成对数据库的操作
2个回答
展开全部
首先用myeclipse创建一个web 工程
这是jsp登录页面
<body>
<%
request.setCharacterEncoding("GBK");
String name=request.getParameter("文件名");
if(name.equals("sa")){
session.setAttribute("UserName",name);
response.sendRedirect("文件名");
}
else{
response.sendRedirect("文件名");
}
%>
</body>
连接数据库文件
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();
}
}
}
这是jsp登录页面
<body>
<%
request.setCharacterEncoding("GBK");
String name=request.getParameter("文件名");
if(name.equals("sa")){
session.setAttribute("UserName",name);
response.sendRedirect("文件名");
}
else{
response.sendRedirect("文件名");
}
%>
</body>
连接数据库文件
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询