当前位置: 技术问答>java相关
关于JDBC连接Sql Server的问题。
来源: 互联网 发布时间:2017-05-01
本文导语: 软件:JDK 1.4 数据库:sql 2000 数据库名字:mydb JDBC for sql驱动安装在C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClib下。 classpath设置:classpath=.;e:billyprogramjava;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBCl...
软件:JDK 1.4 数据库:sql 2000
数据库名字:mydb
JDBC for sql驱动安装在C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClib下。
classpath设置:classpath=.;e:billyprogramjava;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmssqlserver.jar;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmsutil.jar;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmsbase.jar
程序代码如下:
import java.io.*;
import java.sql.*;
class SimpleJDBC{
public static void main(String[] args) throws SQLException,IOException,ClassNotFoundException{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sdURL="jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=mydb";
Connection myConnection=DriverManager.getConnection(sdURL);
if(myConnection==null){
System.out.println("Cound not establish connection!");
}else {
System.out.println("Connection successfully establish!");
Statement myStatement=myConnection.createStatement();
int rc=0;
rc=myStatement.executeUpdate("DROP TABLE EMPLOYEE");
rc=myStatement.executeUpdate("CREATE TABLE Employee("
+"EmpId INTEGER,LastName CHAR(30),"
+"FirstName CHAR(30),YearSvc INTEGER,"
+"BirthDate DATE,Gender CHAR(1),"
+"DeptId INTEGER,QualId INTEGER,"
+"JoiningDate DATE,Comments VARCHAR(200),"
+"SalaryId INTEGER)"
);
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(1,'Blake','James','M',1,3)" );
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(2,'Washington','Mashona','F',3,7)"
);
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(3,'Rosset','Marc','M',1,9)"
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(4,'Stevenson','Alexandra','F',1,4)"
ResultSet rset=myStatement.executeQuery("SELECT LastName,"
+"FirstName,Gender FROM Employee WHERE DeptId=1");
while(rset.next()){
String firstname=rset.getString("FirstName");
String lastname=rset.getString("LastName");
String gender=rset.getString("Gender");
System.out.println(lastname+","+firstname+","+gender);
}
rset.close();
myStatement.close();
myConnection.close();
}
}
}
编译可通过,运行时错误:
C:>java SimpleJDBC
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.
sqlserver.SQLServerDriver
ODBC那里也添加了:数据源名 mydb/服务器 localhost
请问还有什么东西不对劲吗?(我用access测试过程序,能正常运行)。
谢谢
数据库名字:mydb
JDBC for sql驱动安装在C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClib下。
classpath设置:classpath=.;e:billyprogramjava;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmssqlserver.jar;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmsutil.jar;C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClibmsbase.jar
程序代码如下:
import java.io.*;
import java.sql.*;
class SimpleJDBC{
public static void main(String[] args) throws SQLException,IOException,ClassNotFoundException{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sdURL="jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=mydb";
Connection myConnection=DriverManager.getConnection(sdURL);
if(myConnection==null){
System.out.println("Cound not establish connection!");
}else {
System.out.println("Connection successfully establish!");
Statement myStatement=myConnection.createStatement();
int rc=0;
rc=myStatement.executeUpdate("DROP TABLE EMPLOYEE");
rc=myStatement.executeUpdate("CREATE TABLE Employee("
+"EmpId INTEGER,LastName CHAR(30),"
+"FirstName CHAR(30),YearSvc INTEGER,"
+"BirthDate DATE,Gender CHAR(1),"
+"DeptId INTEGER,QualId INTEGER,"
+"JoiningDate DATE,Comments VARCHAR(200),"
+"SalaryId INTEGER)"
);
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(1,'Blake','James','M',1,3)" );
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(2,'Washington','Mashona','F',3,7)"
);
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(3,'Rosset','Marc','M',1,9)"
rc=myStatement.executeUpdate("INSERT INTO Employee"
+"(EmpId,LastName,FirstName,Gender,DeptId,YearSvc)"
+"VALUES(4,'Stevenson','Alexandra','F',1,4)"
ResultSet rset=myStatement.executeQuery("SELECT LastName,"
+"FirstName,Gender FROM Employee WHERE DeptId=1");
while(rset.next()){
String firstname=rset.getString("FirstName");
String lastname=rset.getString("LastName");
String gender=rset.getString("Gender");
System.out.println(lastname+","+firstname+","+gender);
}
rset.close();
myStatement.close();
myConnection.close();
}
}
}
编译可通过,运行时错误:
C:>java SimpleJDBC
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.
sqlserver.SQLServerDriver
ODBC那里也添加了:数据源名 mydb/服务器 localhost
请问还有什么东西不对劲吗?(我用access测试过程序,能正常运行)。
谢谢
|
可能是路径名称太长了,例如:
C:Program FilesMicrosoft SQL Server 2000 Driver for
JDBClibmsutil.jar
把他copy到c:lib试试
C:Program FilesMicrosoft SQL Server 2000 Driver for
JDBClibmsutil.jar
把他copy到c:lib试试
|
首先你用的连接方式是JDBC而不是JDBC—ODBC,所以你设置的ODBC是没有用的。
其次,你的程序中报的错误是JDBC的驱动找不到,去找个地方下载吧。
com.microsoft.jdbc.sqlserver.SQLServerDriver就是这个包
其次,你的程序中报的错误是JDBC的驱动找不到,去找个地方下载吧。
com.microsoft.jdbc.sqlserver.SQLServerDriver就是这个包