java如何连接sql server 2005
我第一次用sql server 2005,做一个学生管理程序
但是连接sql server 2005时总是提示为:
j
ava.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at p1.Connector.<init>(Connector.java:8)
at p1.Connector.main(Connector.java:30)
我的代码为:
import java.sql.Connection;
import java.sql.DriverManager;
public class Connector {
public static Connection conection = null;
public Connector(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conection = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student ","sa","");
// conection = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.1.43:1433;DatabaseName=DB_Student ","sa","");
System.out.println("数据库操作成功,祝贺你");
}catch(
java.lang.ClassNotFoundException classnotfound){
classnotfound.printStackTrace();
}catch(
java.sql.SQLException sql){
sql.printStackTrace();
}
}
// private void closeCon(){
// try {
// conection.close();
// } catch (Exception e) {
// // TODO: handle exception
// }
public static void main(String []args){
Connector con=new Connector();
}
}
------解决方案--------------------是web项目吗?如果是直接把驱动jar放在WebRoot\WEB-INF\lib下就可以。如果是j2se项目,你就在项目名上点右键->Build Path->Configure Build Path->Libraries->Add External JARS 找到你放了驱动jar包得文件夹就OK。
完事了按F5刷新下
------解决方案--------------------你用的是SQLServer2005,但是用的URL 是2000的
你把
Java code
conection = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student ","sa","");