1.建立web 工程
输入Project name.然后点击finish。
2.右键点击WebContent-New-JSP File,新建jsp文件。
3.然后编写一个jsp登录的程序
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录界面</title>
</head>
<body>
<center>
<font color="BLUE">登录界面</font>
<form action="NewFile1.jsp" method="post">
用户:<input type="text" name = "username"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" value="提交">
</form></center>
</body>
</html>
4.然后我们我们要编写一个java代码确定数据库已链接eclipse
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String [] args)
{
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Text";
String userName="sa";
String userPwd="20153121";
try
{
Class.forName(driverName);
Connection ConnectiondbConn = DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接数据库成功");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("连接失败");
}
}
}
<%@ page language="java" import="java.sql.*" import="java.util.*" contentType="text/html;; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<%
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Text";
String userName="sa";
String userPwd="20153121";
Class.forName(driverName);
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
String sql = "select * from Table_1 where [name]=? and [password]=?";
PreparedStatement pstmt = dbConn.prepareStatement(sql);
request.setCharacterEncoding("UTF-8");
String us = request.getParameter("username");
String pass = request.getParameter("password");
pstmt.setString(1, us);
pstmt.setString(2, pass);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
%><center><h1>登陆成功!</h1></center><%
}
else
{
%><center><h1>登陆失败!</h1></center><%
}
%>
</body>
<ml>
6.步骤完成,可以完成一个带有数据库的登录界面。
通过这门课的学习,希望对软件开发有更多的了解,希望可以开发出一个小小的软件。计划每周花费4个小时来学习。