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個小時來學習。