当前位置导航:炫浪网>>网络学院>>编程开发>>MsSQL教程

JTDS连接SQL Server中的方法示例


  <%@ page import="java.io.*"%>
  <%@ page import="java.util.*"%>
  <%@  page import="java.sql.*"%>
  <%@ page import="net.sourceforge.jtds.jdbc.Driver.*"%>
  <%@ page contentType="text/html;charset=gb2312"%>
  <html>
  <head><title></title></head>
  <body>
  <%
  try{
  Connection con;
  Statement stmt;
  ResultSet rs;
  
  Class.forName("net.sourceforge.jtds.jdbc.Driver");
  DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
  String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/j2eeone;user=sa;password=caicaibao";
  //String dbUser = "j2ee";
  //String dbPwd = "j2ee";
  
  con = java.sql.DriverManager.getConnection(dbUrl);
  
  stmt = con.createStatement();
  rs = stmt.executeQuery("SELECT id,name,title,price FROM books");
  
  out.println("<table border='1' width='400'>");
  while(rs.next()){
  String col1 = new String(rs.getString(1).getBytes("ISO8859-1"));
  String col2 = rs.getString(2);
  String col3 = rs.getString(3);
  String col4 = rs.getString(4);
  
  out.println("<tr><td>" + col1 + "</td><td>" + col2 + "</td><td>" + col3 + "</td><td>" + col4 + "</td></tr>");
  }
  
  out.println("</table>");
  
  rs.close();
  stmt.close();
  con.close();
  }catch(Exception e){
  out.println(e.getMessage());
  }
  %>
  </body>
  </html>
  
  JTDS下载地址:http://sourceforge.net/project/showfiles.php?group_id=33291
  上面通过环境:winxpsp2,java1.4.2,sql server2000 sp3, jtds1.1
  注意jtds0.9.1以下版本在使用中文时可能会有乱码
相关内容
赞助商链接