qiek(原作)
据说微软提供的jdbc驱动不支持反复查询所以用netsourceforgejtdsjdbcDriver()
在WinServer上安装SQLSERVER必须安装补丁sp
写类时注意JAVA中CLASSFORNAME方法必须是一个可以捕获的错误把它放在TRY{}CATCH(CLASSNOTFOUNTEXCEPTION){}中就不会出错了
另CLASS_PATH中必须加入jtdsxxxjar的完整路径名如\;;D:\jsdk_\jre\lib\ext\jtdsjar;
/*
* Author:liufei
* Homepage:
* Copyright henan SUNCER software Inc All rights reserved
*/
import javasql*;
import javautil*;
class Sql {
public static void main(String[] args) {
try{
String query=select * from Orders;
String URL=jdbc:jtds:sqlserver://:/Northwind;USER=sa;PASSWORD=webserver;
ClassforName(netsourceforgejtdsjdbcDriver);
Connection conn=DriverManagergetConnection(URL);
Statement stmt=conncreateStatement(ResultSetTYPE_SCROLL_SENSITIVEResultSetCONCUR_UPDATABLE);
ResultSet rs=stmtexecuteQuery(query);
int i=;
for(i=;i <10;i++){
rs.next();
System.out.println(rs.getString(1));
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
用JSP连接:
必须将jtds.jar放至WEB-INF/lib目录下
test.jsp内容如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String url="jdbc:jtds:sqlserver://135.0.0.2:1433/Northwind;USER=sa;PASSWORD=webserver";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from Orders";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>