Eclipse连接SqlServer2000数据库的问题
importjava.sql.SQLException;importjava.sql.*;importcom.microsoft.jdbc.sqlserver.SQLSe...
import java.sql.SQLException;
import java.sql.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
/**
*
*/
/**
* 第一个JDBC的HelloWorld程序,数据库访问sql server
* @author Administrator
*@version 0.1 2009-11-12
*/
public class JDBCHelloWorld {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
//1注册驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();//sql server驱动
}
//2获取数据库联接
java.sql.Connection conn=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
//3获取表达式
java.sql.Statement stmt=conn.createStatement();
//4读取数据
java.sql.ResultSet rs=stmt.executeQuery("select * from user");
//5显示结果集里面的数据
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println();
}
//6释放资源
rs.close();
stmt.close();
conn.close();
}
}
java.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 JDBCHelloWorld.main(JDBCHelloWorld.java:23)
Exception in thread "main" java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source) 展开
import java.sql.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
/**
*
*/
/**
* 第一个JDBC的HelloWorld程序,数据库访问sql server
* @author Administrator
*@version 0.1 2009-11-12
*/
public class JDBCHelloWorld {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
//1注册驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();//sql server驱动
}
//2获取数据库联接
java.sql.Connection conn=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
//3获取表达式
java.sql.Statement stmt=conn.createStatement();
//4读取数据
java.sql.ResultSet rs=stmt.executeQuery("select * from user");
//5显示结果集里面的数据
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println();
}
//6释放资源
rs.close();
stmt.close();
conn.close();
}
}
java.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 JDBCHelloWorld.main(JDBCHelloWorld.java:23)
Exception in thread "main" java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source) 展开
1个回答
展开全部
package conn;
import java.sql.*;
public class Conn {
Connection conn=null;
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Connection connn=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=test","sa","sa");
Statement stmt=connn.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
}
}
import java.sql.*;
public class Conn {
Connection conn=null;
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Connection connn=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=test","sa","sa");
Statement stmt=connn.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询