Servlet中JDBC浅显实例

  1: import javax.servlet.*;  //servlet文件必须引入的包
  2: 
  3: import javax.servlet.http.*;  //servlet文件必须引入的包
  4: 
  5: import java.io.*;   //因为要抛出IO异常,所以引入IO包
  6: 
  7: import java.sql.*;  //因为要联接数据库,所以需要引入sql的?
  8: 
  9:  
 10: 
 11: public class ConSql extends HttpServlet
 12: 
 13:                                                  //新建的servlet应用程序必须继承HttpServlet类
 14: 
 15: {
 16: 
 17:     public void init(ServletConfig config) throws ServletException
 18: 
 19:        //使用ServletConfig config对象对新建的Servlet进行初始化设置  
 20: 
 21:   {
 22: 
 23:     super.init(config);
 24: 
 25:            //为确保形参中的config对象能正常进行初始化需要激活其父类的init方法    
 26: 
 27:     try
 28: 
 29:     {
 30: 
 31:       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 32: 
 33:           //连接数据库第一步,在init方法中注册SQL数据库驱动程序是为了
 34: 
 35:             //添加数据库驱动程序
 36: 
 37: //需要抛出异常
 38: 
 39: //联接数据库时最好在这里先注册驱动程序
 40: 
 41:             //在doGet或者doPost方法中再建立数据链接
 42: 
 43:     }
 44: 
 45:     catch(Exception e)
 46: 
 47:     {
 48: 
 49: e.printStackTrace();    
 50: 
 51: System.out.println(“数据库连接异常”);
 52: 
 53:     }
 54: 
 55:   }
 56: 
 57:  public void doGet(HttpServletRequest request, HttpServletResponse response)
 58: 
 59:      throws ServletException, IOException
 60: 
 61:  {
 62: 
 63:     response.setContentType("text/html; charset=gb2312");
 64: 
 65:     //设置返回的内容类型形式
 66: 
 67:     PrintWriter out = response.getWriter();
 68: 
 69:     //创建一个文本打印输出流
 70: 
 71:     
 72: 
 73:     /*
 74: 
 75:     out.println("<html>");
 76: 
 77:     out.println("<head><title>网页标题</title></head>");
 78: 
 79:     out.println("<body ">");
 80: 
 81:     out.println("网页内容");
 82: 
 83:     out.println("</body></html>");
 84: 
 85:    //用out.println动态的生成一个网页
 86: 
 87:     */
 88: 
 89:     try
 90: 
 91:     {
 92: 
 93:            String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
 94: 
 95:                   //      jdbc是主协议
 96: 
 97:                   //           microsoft:sqlserver是子协议
 98: 
 99:                   //                          localhost是本机名(或IP)
100: 
101:                   //                                           1433端口为SQL默认端口 
102: 
103:                   
104: 
105:            Connection con=null;
106: 
107:             con=DriverManager.getConnection(url,"sa","");
108: 
109:             //连接数据库第二步
110: 
111:                   //用Connection对象与数据库建立一个联接
112: 
113:                   //sa是访问数据库的用户口令
114: 
115:                   //sa后面的""是访问数据库的密码
116: 
117:                   //在这里需要抛出一个异常
118: 
119:             
120: 
121:              Statement sta=null;
122: 
123:             sta=con.createStatement();
124: 
125:             //连接数据库第三步
126: 
127:             //创建一个操纵SQL语句的对象
128: 
129:              
130: 
131:             ResultSet res=sta.executeQuery("SELECT * FROM student");
132: 
133:            //res对象接收select返回的结果集
134: 
135:            //查询用executeQuery("select * from 表名")
136: 
137:            //增删改用executeUpdate("insert into 表名 values(,,,,)")
138: 
139:         while(res.next())//res.next()方法判断是否还有下一条记录
140: 
141:         {
142: 
143:             String str1=res.getString("学号");
144: 
145:             //str 得到数据库中id字段
146: 
147:             String str2=res.getString("姓名");
148: 
149:             //str0 得到数据库中name字段
150: 
151:             String str3=res.getString("年龄");
152: 
153:             String str4=res.getString("性别");
154: 
155:             String str5=res.getString("系别");
156: 
157:             out.println(str1);
158: 
159:             out.println(str2);
160: 
161:             out.println(str3);
162: 
163:           out.println(str4);
164: 
165:             out.println(str5);
166: 
167:         }
168: 
169:         res.close();
170: 
171:         sta.close();
172: 
173:         con.close();
174: 
175:         
176: 
177:         }
178: 
179:         catch(Exception e)
180: 
181:         {
182: 
183:             e.printStackTrace();
184: 
185:         }    
186: 
187:   }
188: 
189:   public void doPost(HttpServletRequest request, HttpServletResponse response)
190: 
191:      throws ServletException, IOException
192: 
193:   {
194: 
195:     doGet(request,response);
196: 
197:     //在doPost方法中调用doGet方法,节省代码
198: 
199:   }
200: 
201:   
202: 
203:   public void destroy()
204: 
205:   {
206: 
207:     //垃圾回收
208: 
209:   }
210: 
211: }
212: 
213:  
214: 
215: 其他数据库连接驱动及URL.
216: 
217: //SQL_Server:
218: 
219: String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
220: String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
221: String user = "sa";
222: String password = "";
223: 
224: 
225: //Oracle:
226: 
227: String driver = "oracle.jdbc.driver.OracleDriver";
228: String url = "jdbc:oracle:thin:@localhost:1521:wanchao";
229: String user = "scott";
230: String password = "tiger";
231: 
232: 
233: //mysql
234: String driver="com.mysql.jdbc.Driver";
235: String url="jdbc:mysql://localhost:3306/chinabank?useUnicode=true&amp;characterEncoding=GBK";
236: String user="root";
237: String passeord="root";
238: 
239: Class.forName(driver); 
240: con = DriverManager.getConnection(url,user,password);
241: 
242: 
posted @ 2011-09-27 14:53  chenatu  阅读(466)  评论(0编辑  收藏  举报