地质储量采油速度:java访问SQL SERVER 2000?
我现在安了microsoft sql server 2000 for jdbc.也配置了ODBC(当然不知道是否配置对了)。
但是运行:
import java.sql.*;
public class sqlTest {
public static void main(String []args){
String user="sa";
String password="007";
String ls_1="use master"+"go"+"select * from employee";
try{
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
Connection con=DriverManager.getConnection("master",user,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(ls_1);
System.out.print("firstname ");
System.out.print("lastname ");
System.out.print("age ");
System.out.print("address ");
System.out.print("city ");
while(rs.next()){
System.out.print(rs.getString(1)+" | ");
System.out.print(rs.getString(2)+" | ");
System.out.print(rs.getInt(3)+" | ");
System.out.print(rs.getString(4)+" | ");
System.out.print(rs.getString(5));
}
rs.close();
stmt.close();
con.close();
}
catch(SQLException sqle){
System.out.print(1+sqle.toString());
}
catch(Exception e){
System.out.println(2+e.toString());
}
}
}
报错:classNotFoundException:sun.jdbc.odbc.jdbcOdbcDriver
请JAVA高手具体指点。
安装jdbc驱动后不需要配置odbc,安装之后,打开那个文件夹,你会看到三个jar包,只要把这三个jar包设置到环境变量(classpath)里就可以了,象这样
假设你安在d盘的sqldriver目录下,那么就是
d:\sqldriver\msbase.jar;d:\sqldriver\mssqlserver.jar;d:\sqldriver\msutil.jar;
==================================================
import java.sql.*;
public class SqlTesting {
public static void main(String args[]) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user ="sa";
String password = "sa";
String sqlStr = "select CustomerID, CompanyName, ContactName from Customers";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println( "ÀàʵÀý»¯³É¹¦!" );
Connection con = DriverManager.getConnection( url, user, password );
System.out.println( "´´½¨Á¬½Ó¶ÔÏñ³É¹¦!" );
Statement st = con.createStatement();
System.out.println( "´´½¨Statement³É¹¦!" );
ResultSet rs = st.executeQuery( sqlStr );
System.out.println( "²Ù×÷Êý¾Ý±í³É¹¦!" );
System.out.println( "----------------!" );
while(rs.next()) {
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
} catch(Exception err) {
err.printStackTrace(System.out);
}
}
}