实践1. tomcat+myeclipse+sql2000 登陆

看了很久的书,也工作了一段时间。昨天同学要我帮他做一个jsp登陆,弄了很久才做七七八八,光看不练是不行的,有必要动手做做了。

环境:tomcat6.0、myeclipse6.1、sql2000、jdk1.6

安装jdk1.6,在myeclipse中 MyEclipse-->preferences..-->servers-->tomcat6.0选择enable,输入tomcat的路径到第一层。再在一开始tomcat下面点击“+”,输入jdk的路径。

新建登陆页面Login/WebRoot/index,jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登陆</title>
</head>
<script language="javascript">
function checkLogin(){	
	if(document.loginInf.login_id.value == "" || document.loginInf.login_id.value == null ){
		alert("账号不能为空,请重新输入!");
		return false;
	}
	if(document.loginInf.login_password.value == "" || document.loginInf.login_password.value == null ){
		alert("密码不能为空,请重新输入!");
		return false;
	}
	return true;
	}
</script>

<body align="center">
<form name="loginInf" action="loginBO.jsp" method="post" onSubmit="return checkLogin()">
 <table width="300px" align="center">
   <tr width="280px">
     <td width="80px" align="right"><font size="2">账号:</font></td>
     <td width="200px" align="left"><input name="login_id" id="login_id" type="text" style="width:180"/></td>
   </tr>
     <tr width="300px">
     <td width="80px" align="right"><font size="2">密码:</font></td>
     <td width="200px" align="left"><input name="login_password" type="login_password" style="width:180"/></td>
   </tr>
   <tr width="300px">
     <td width="125px" align="right"><input type="reset" value="重置"/></td>
    <td width="125px" align="center"><input type="submit" name="submit" value="提交"/></td>
   </tr>
</body>
<html>

新建登陆处理页面Login/WebRoot/loginBO.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="Dao.DB" %>
<%@ page import="Bean.LoginInfBean" %>

<jsp:useBean id="DB" class="Dao.DB" scope="session"/>
<jsp:useBean id="user" class="Bean.LoginInfBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<html>
<body>
<h3>用户登录验证</h3>
<% 
	String login_id = user.getLogin_id();
	String login_password = user.getLogin_password();
	String sql = "select top 1 * from login_inf where login_password='"+login_password+"'and login_id='"+login_id+"'";
	if(DB.query(sql) != null){
	         out.print("登陆成功!");
	}else{
		out.print("用户为空,或密码错误!");
	}
%>
</body>
</html>

新建Bean,Login/src/Bean/LoginInfBean.java

package Bean;

public class LoginInfBean {
	
	private int login_code;
	private String login_id;
	private String login_password;
	
	public int getLogin_code() {
		return login_code;
	}
	public void setLogin_code(int login_code) {
		this.login_code = login_code;
	}
	public String getLogin_id() {
		return login_id;
	}
	public void setLogin_id(String login_id) {
		this.login_id = login_id;
	}
	public String getLogin_password() {
		return login_password;
	}
	public void setLogin_password(String login_password) {
		this.login_password = login_password;
	}
	
}

新建Dao,连接数据库及对对表的操作Logi/src/Dao/DB.java

 

package Dao;

import	java.sql.*;

public class DB {
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	public DB(){};
	
	public Connection getConn(){
	  try{
	    /*****
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    String url = "jdbc:mysql://localhost:3306/Qing";
	    String user = "sa";
	    String password = "123456";
	    *****/
	    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
	    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Qing";
	    String user = "sa";
	    String password = "123456";
	    conn = DriverManager.getConnection(url,user,password);
	  }catch(Exception ex){
	    ex.printStackTrace();
	  }
	  return conn;
	}
	
	/**
	 * 
	 * @param sql
	 * @return  查询结果
	 */
	public ResultSet query(String sql){
	  try{
	    conn = getConn();
	    stmt = conn.createStatement();
	    rs = stmt.executeQuery(sql);
	  }catch(Exception ex){
	    ex.printStackTrace();
	  }
	  return rs;
	}
	
	public void update(String sql){
	  try{
	    conn = getConn();
	    stmt = conn.createStatement();
	    stmt.executeUpdate(sql);
	  }catch(Exception ex){
	    ex.printStackTrace();
	  }
	}
	
	public void update(String sql,String[] args){
	 try{
	    conn = getConn();
	    pstmt = conn.prepareStatement(sql);
	    for(int i=0; i<args.length ;i++){
	    pstmt.setString(i+1, args[i]);
	  }
	    pstmt.executeUpdate();
	  }catch(Exception ex){
	    ex.printStackTrace();
	  }
	}
	
	public void closeDB(){
	try{
	  if(rs != null){
	    rs.close();
	    rs = null;
	  }
	  if(stmt != null){
	    stmt.close();
	    stmt = null;
	  }
	  if(pstmt != null){
	    pstmt.close();
	    pstmt = null;
	  }
	  if(conn != null){
	    conn.close();
	    conn =null;
	  }
	  }catch(Exception ex){
	    ex.printStackTrace();
	  }
	}
	
}

 到这代码完成,到数据库建表。打开tomcat,http://127.0.0.1:8080/Login就可以登陆

你可能感兴趣的:(sql,tomcat,exception,String,MyEclipse,login)