首先在此之前我们应该正确安装数据库,以及eclipse(javaee)文件包。
1.首先在javaee中建立新的项目
2.右键点击WebContent-New-JSP File,新建jsp(动态)文件。(想在哪个文件夹里添加jsp文件,就直接右击文件夹新建,注意一定不要展开文件夹,否则容易出现路径不正确的问题)
3.进行页面的编写
login.jsp
logincheck.jsp
Insert title hereResultSet rs=db.executeQuery(sql);//运行上面的语句if(rs.next())
{/* if(password.equals(rs.getString(2)))
{
}*/
if(password.equals(rs.getObject("password"))){
response.sendRedirect("loginsuccess.jsp");
}else{
out.print("");
response.setHeader("refresh","0;url=login.jsp");
}
}else{
out.print("");
response.setHeader("refresh","0;url=login.jsp");
}%>
loginsuccess.jsp
Insert title here登录成功
创建DBBean.java,连接数据库
package Bean;
import java.sql.*;
public class DBBean {
private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=z";
private String dbusername = "ZH";
private String dbpassword = "123";
private Connection conn = null;
private Statement stmt = null;
public DBBean()
{
try
{
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
stmt = conn.createStatement();
}
catch (Exception ex) {
System.out.println("数据连接失败!");
}
}
public int executeUpdate(String s) {
int result = 0;
System.out.println("--更新语句:"+s+"\n");
try {
result = stmt.executeUpdate(s);
} catch (Exception ex) {
System.out.println("执行更新错误!");
}
return result;
}
public ResultSet executeQuery(String s) {
ResultSet rs = null;
System.out.print("--查询语句:"+s+"\n");
try {
rs = stmt.executeQuery(s);
} catch (Exception ex) {
System.out.println("?执行查询错误!");
}
return rs;
}
public void execQuery(String s){
try {
stmt.executeUpdate(s);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行插入错误!");
}
}
public void close() {
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
}
}
4.进行程序执行,最后登录成功!
原文:http://www.cnblogs.com/zhangjiabei/p/6464147.html