概述
首先,我们必须下载JDBC Driver(我用的是SqlServer 2008),可以到微软的站上去下载,这是下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyID=99b21b65-e98f-4a61-b811-19912601fdc9&displaylang=zh-cn 下载我们将得到一个自解压的压缩包,随便找个目录把这个压缩包解压。注意:在解压的目录名字中不要存在空格(在配置环境变量是有用)。据微软说,这个环境变量的设置中间如果存在空格有可能导致环境变量设置失败,谨慎起见,我们还是将jdbc解压到没有空格的目录里。在解压目录下,我们会看到sqljdbc_2.0chs,在chs下面,我们可以看到两个jar,一个时sqljdbc.jar,另一个是sqljdbc4.jar。接下来,我们把这个jar加到环境变量的CLASSPATH中,如:
这个是我的环境变量的设置。注意:sqljdbc.jar 和sqljdbc4.jar包含一个到环境变量就可以了,不要将两个都加到CLASSPATH中。
仅仅在系统的环境变量中添加CLASSPATH还不够,还需要在IDE中添加环境变量。我是用NetBeans的,添加情况如下:首先在项目上右击,选择属性:
在库中添加sqljdbc4.jar或者sqljdbc.jar
OK配置完成,接下来就是测试了。在SqlServer 2008中新建数据库名为TestDataBase,在数据库中添加Users表,Users表中有UserId,UserName两项,在表内填入一些数据。
如下式测试代码:
1: /*
2: * To change this template, choose Tools | Templates
3: * and open the template in the editor.
4: */
5:
6: package com.mlp;
7:
8: import com.mlp.classes.*;
9: import java.io.IOException;
10: import java.io.PrintWriter;
11: import javax.servlet.ServletException;
12: import javax.servlet.http.HttpServlet;
13: import javax.servlet.http.HttpServletRequest;
14: import javax.servlet.http.HttpServletResponse;
15:
16: /**
17: *
18: * @author Administrator
19: */
20: public class Servlet extends HttpServlet {
21:
22: /**
23: * Processes requests for both HTTPGET
andPOST
methods.
24: * @param request servlet request
25: * @param response servlet response
26: * @throws ServletException if a servlet-specific error occurs
27: * @throws IOException if an I/O error occurs
28: */
29: protected void processRequest(HttpServletRequest request, HttpServletResponse response)
30: throws ServletException, IOException {
31: response.setContentType("text/html;charset=UTF-8");
32: PrintWriter ut = response.getWriter();
33: try {
34: String id = "123";
35: String name="mlp";
36: UserBean ub = new UserBean();
37: if(ub.CheckUser(id, name))
38: {
39: out.println("合法用户。");
40: }
41: else
42: {
43: out.println("非法用户。");
44: }
45: } finally {
46: out.close();
47: }
48: }
49:
50: //
51: /**
52: * Handles the HTTP GET
method.
53: * @param request servlet request
54: * @param response servlet response
55: * @throws ServletException if a servlet-specific error occurs
56: * @throws IOException if an I/O error occurs
57: */
58: @Override
59: protected void doGet(HttpServletRequest request, HttpServletResponse response)
60: throws ServletException, IOException {
61: processRequest(request, response);
62: }
63:
64: /**
65: * Handles the HTTP POST
method.
66: * @param request servlet request
67: * @param response servlet response
68: * @throws ServletException if a servlet-specific error occurs
69: * @throws IOException if an I/O error occurs
70: */
71: @Override
72: protected void doPost(HttpServletRequest request, HttpServletResponse response)
73: throws ServletException, IOException {
74: processRequest(request, response);
75: }
76:
77: /**
78: * Returns a short description of the servlet.
79: * @return a String containing servlet description
80: */
81: @Override
82: public String getServletInfo() {
83: return "Short description";
84: }//
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16436858/viewspace-630268/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16436858/viewspace-630268/
最后
以上就是闪闪烧鹅为你收集整理的让我揣摩不透的JDBC !的全部内容,希望文章能够帮你解决让我揣摩不透的JDBC !所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复