WEB开发网
开发学院数据库MSSQL Server JTDS连接SQLServer中的方法示例 阅读

JTDS连接SQLServer中的方法示例

 2007-11-11 08:48:12 来源:WEB开发网   
核心提示:<%@ page import="java.io.*"%><%@ page import="java.util.*"%><%@page import="java.sql.*"%><%@ page import=&quo

  <%@ 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 server(WINDOWS平台上强大的数据库平台)2000 sp3, jtds1.1
  注意jtds0.9.1以下版本在使用中文时可能会有乱码

Tags:JTDS 连接 SQLServer

编辑录入:coldstar [复制链接] [打 印]
赞助商链接