• JDBC连接SQL Server数据库


    测试环境

    数据库:SQL Server 2008 R2,创建数据库名:TestDemo,表:User,字段如下:

      字段 字段
    id UName UPass


    sqljdbc.jar下载地址:
    依赖的Jar包:sqljdbc.jar

    http://download.csdn.net/download/wu996489865/9375672

     

    step1  将下载后的jar包放在WebRoot/WEB-INF/lib文件夹

    step2  创建两个简单的页面进行测试,分别为index.jsp(注册页)和insert.jsp(处理页)
    index.jsp(注册页)代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <form action="insert.jsp" method="post">
        用户名:<input type="text" name="name"/><br>
        密码:<input type="password" name="pass" /><br>
        <input type="submit" name="submit" value="注册"/>
        </form>
      </body>
    </html>

    step3  insert.jsp(处理页)代码:

    <%@ page language="java" import="java.sql.*" 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>Insert title here</title>
    </head>
    <body>
    
    <%
        try
        {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TestDemo","sa","luotian");
        Statement state=conn.createStatement();
        int result=state.executeUpdate("insert into [User](UName,UPass) values('"+request.getParameter("name")+"','"+request.getParameter("pass")+"')");
        if(result==1)
            out.println("用户注册成功");
        else
            out.println("用户注册失败");
        }
            catch(SQLException e)
            {
                out.println(e);
            }    
     %>
    </body>
    </html>
  • 相关阅读:
    P3225 [HNOI2012]矿场搭建 题解
    CodeForces
    poj-3723
    codeforces -1214 E
    POJ-1741 树上分治--点分治(算法太奇妙了)
    洛谷p1345---最小割的奇妙运用
    洛谷p2149----两个终点和两个起点,最短路最大交汇长度!!!
    BerOS File Suggestion(字符串匹配map)
    Garbage Disposal(模拟垃圾装垃圾口袋)
    第八周组队赛
  • 原文地址:https://www.cnblogs.com/tinaluo/p/8303868.html
Copyright © 2020-2023  润新知