尝试使用Java连接到SQL Server 2005时出现ClassNotFoundException
我对数据库管理还不熟悉。我只是想连接到数据库并在命令提示符中检索并显示一个表。数据库不在我的电脑上。我很确定网址是问题所在。代码:
I'm fairly new to database management. I'm just trying to connect to the database and retrieve and display a table in the command prompt. The database is not on my computer. I am fairly certain that the url is the problem. The code:
import java.io.*;
import java.sql.*;
class transfer{
//driver and DB URLs
final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final static String DB_SQL = "jdbc:microsoft:sqlserver://localhost:1433;" + "database=DataDB;" + "user=sa;" + "password=1234";
//Database Username and password
final static String user1 = "sa";
final static String pass1 = "1234";
static ResultSet rs;
public static void main(String args[]) throws SQLException, ClassNotFoundException{
Connection conn_sql = null;
Statement stmt_sql = null;
//Statement stmt_ora = null;
//Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Open Connection
System.out.println("Connecting to SQL database...");
conn_sql = DriverManager.getConnection(DB_SQL, user1, pass1);
//Execute Query
String sql_query;
System.out.println("Creating statement for SQL...");
stmt_sql = conn_sql.createStatement();
sql_query = "Select * from attendancesummary";
rs = stmt_sql.executeQuery(sql_query);
System.out.println("SQL table details");
System.out.println("Creating statement for SQL...");
while(rs.next()){
//Retrieve data
int cno = rs.getInt("CardNo");
int in_time = rs.getInt("entry");
int out_time = rs.getInt("Exittm");
String name = rs.getString("Name");
int date = rs.getInt("TrDate");
//Display data
System.out.print("Employee ID: "+cno);
System.out.print("\tName: "+name);
System.out.print("\tDate:"+date);
System.out.print("\tEntry: "+in_time);
System.out.print("\tExit: "+out_time);
}
}
}
数据库名称是DataDB,我想要检索和显示的表是READancesummary。我已将路径设置为C:\Program Files \ Java @\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ enu \sqljdbc4.jar
The database name is DataDB and the table that I want to retrieve and display is attendancesummary. I have set my path as "C:\Program Files\Java\jdk1.8.0_11\bin";"C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar"
代码编译好..但是当我运行它时,我收到以下错误:
The code compiles fine.. but when I run it, I get the following error:
线程中的异常mainjava.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader $ 1.run
at java.net.URLClassLoader $ 1 .run
java.security.AccessController.doPrivileged
at java.net.URLClassLoader.findClass
at java.lang.ClassLoader.loadClass
at sun.misc.Launcher $ AppClassLoader .loadClass
at java.lang.ClassLoader.loadClass
at java.lang.Class.forname0
at java.lang.Class.forname
at transfer.main
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.net.URLClassLoader$1.run at java.net.URLClassLoader$1.run at java.security.AccessController.doPrivileged at java.net.URLClassLoader.findClass at java.lang.ClassLoader.loadClass at sun.misc.Launcher$AppClassLoader.loadClass at java.lang.ClassLoader.loadClass at java.lang.Class.forname0 at java.lang.Class.forname at transfer.main
我真的输了。任何帮助将不胜感激!
I am truly lost. Any help would be appreciated!
这意味着 sqljdbc4.jar
运行代码时, CLASSPATH 中缺少此内容。如果从命令行运行此命令,则在 -cp
中将 sqljdbc4.jar
的路径添加到 java
命令。
It means that sqljdbc4.jar
is missing from your CLASSPATH while running the code. If you are running this from command line then add the path to sqljdbc4.jar
in -cp
switch of java
command.
如果从eclipse运行,则添加 sqljdbc4.jar
在您的构建路径中。
If you are running from eclipse, then add sqljdbc4.jar
in your build path.