jsp与数据库连接实例:数据库课程设计冰山一角(完整模块)

来源:互联网 发布:手机游戏编程软件 编辑:程序博客网 时间:2024/05/09 13:49

      在网上学习过很多人做的关于jsp连接各类数据库,我也看了一下。主要是为了课程设计。当然在总的课程设计中我做的是jsp也就是课程的冰山一角。不管怎样对于一个决定学习java的人来说接触这方面的东西是迟早的事情。当然这里面也许存在一些问题,希望能与爱好或者借鉴此段程序的人一起讨论。

       这段程序主要是对三种情况数据经行验证,准确的说就是验证系统的一部分。

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
 <%@page import="java.text.SimpleDateFormat"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.String"%>
<%@ page import="java.util.Date"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
    //sql  connection连接数据库
    try{
     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    }catch(ClassNotFoundException e){
     throw new ClassNotFoundException("SQL qu dong jia zai shi bai!");//没办法英语没学好只能用拼音,据说全英文的正规
    }
 String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=RegData";
 String user1="sa";
 String password="xxxxxx";
    String user=request.getParameter("UserName");
    String id=request.getParameter("HardId");
    String code=request.getParameter("RegCode");
    try{
  Connection conn= DriverManager.getConnection(url,user1,password);
  Statement stmt=conn.createStatement();
  String sql="select * from RegCode where RegName = '"+ user+"' and MachineID='"+
  id +"' and RegCode='"+ code+"' "; 
  ResultSet rs=stmt.executeQuery(sql);
  //ERROR103
  if(rs.next())
  {
    out.println("ERROR103");
   
  }
  //ERROR102
  sql="select * from BlackList where RegName = '"+ user +"' and MachineID='"+ id +"'";
  rs=stmt.executeQuery(sql);   
      if(rs.next())
   {
      out.println("ERROR102");    
   }
    //ERROR101  
      if((user=="")|(id=="")|(code==""))
      {
        out.println("ERROR101");
      }
    //Modify  time
     Date now=new Date();
     SimpleDateFormat formater=new SimpleDateFormat("yyyy/MM/dd");
     String strCurrentTime=formater.format(now);
  sql="insert into answers(user,now)values('"+user+"', '"+strCurrentTime+"')" ;
  rs=stmt.executeQuery(sql);
  //safe loading
     if(rs.next()){
   out.println("Safe Code");   
     }
     rs.close();
  stmt.close();
  conn.close();
    }catch(SQLException e){
     throw new SQLException("shujuku  error");
    } 
   
//http://localhost/db.jsp?UserName=user2&HardID=machine2&RegCode=reg2
//http://localhost/db.jsp?UserName=user1&HardID=machine1&RegCode=reg1
%>
</body>
</html>

原创粉丝点击