注意,sql server 2000必须是sp3
用jdbc访问数据库
(1) 该实例已经运行通过
jsp连接Sql Server7.0/2000数据库
testsqlserver.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MDB"; //MDB为你的数据库的
String user="sa";
String password="123456";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1);%>
您的第二个字段内容为:<%=rs.getString(2);%>
<%}%>
<%out.print("数据库操作成功,哈哈");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
(2)java访问sqlserver服务器
第一步:安装jdbc
点击SQL Server for JDBC驱动程序安装程序setup.exe(可以到微软网站下载 http://msdn.microsoft.com/library/default.asp?rul=/downloads/list/sqlserver.asp下载)
第二步:设置系统变量classpath
假设SQL Server for JDBC 驱动程序安装在d:jdbc,则classpath应该设置如下:
classpath:=.;…;d:jdbclib; d:jdbclibmssqlserver.jar; d:jdbclibmsutil.jar; d:jdbclibmsbase.jar;
注意:设置时要在最前面的点号和分号
第三步:编辑java程序并且运行
实例1如下:
import java.sql.*;
import java.net.URL;
class insert
{
public static void main(String[] args)
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind";
String query="select * from categories";
String query1="insert categories values(10,’Hanbao’,’Sweet’)";
String query2="insert categories values(11,’Naicha’,’Coffee taste’)";
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url,"sa","739555");
Statement stmt=con.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.close();
con.close();
}
catch(SQLException ex)
{
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
}
}
实例2如下:
import java.sql.*;
import java.net.URL;
class java2sqlserver
{
public static void main(String[] args)
{
String url="jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=739555;DatabaseName=northwind";
String query="Select * From Categories";
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//DriverManager.setLogStream(System.out);
Connection con=DriverManager.getConnection(url);
checkForWarning(con.getWarnings());
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
dispResultSet(rs);
rs.close();
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println(ex.toString()+"—-SQLException caught—-");
while(ex!=null)
{
System.out.print("SQLState:"+ex.getSQLState());
System.out.print("Message:"+ex.getMessage());
System.out.print("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
}
private static boolean checkForWarning(SQLWarning warn)
{
boolean rc=false;
if(warn!=null)
{
System.out.println("—-Warning—-");
rc=true;
while(warn!=null)
{
System.out.print("SQLState:"+warn.getSQLState());
System.out.print("Message:"+warn.getMessage());
System.out.print("Vendor:"+warn.getErrorCode());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc;
}
private static void dispResultSet(ResultSet rs) throws SQLException
{
int i;
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
for(i=1;i<=numCols;i++)
{
if(i>1) System.out.print(", ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
boolean more=rs.next();
while(more)
{
for(i=1;i<numCols;i++)
{
if(i<1) System.out.print(", ");
System.out.println(rs.getString(i));
}
System.out.println("");
more=rs.next();
}
}
//System.out.println("Hello World!");
}