当前位置: 技术问答>java相关
请高手帮忙看看我的程序!
来源: 互联网 发布时间:2015-08-07
本文导语: import java.sql.*; public class MakingTheConnection { public static void main(String[] args) { try { Class.forName("com.microsoft.jdbc.sqlserver.sqlserverDriver"); String url="jdbc:microsoft:sqlserver://127.0.0.1:1422"; String user="sa"; String password=...
import java.sql.*;
public class MakingTheConnection {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.sqlserverDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1422";
String user="sa";
String password="sa";
Connection con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from book");
System.out.println("isbn"+" "+"code"+" "+"thename");
while(rs.next()) {
int a=rs.getInt("isbn");
int b=rs.getInt("code");
String c=rs.getString("thename");
System.out.println(a+" "+b+" "+c);
}
stmt.close();
con.close();
}
catch (ClassNotFoundException e) {
System.err.println("ClassNotFoundException : "+e.getMessage());
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
为什么我运行了后出现ClassNotFoundException : com.microsoft.jdbc.sqlserver.sqlserverDriver
请问是哪里的毛病,应该怎么处理,请高手指点,我在这里都2个多星期了,快疯掉了。谢谢!初学,希望讲的明白点。
public class MakingTheConnection {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.sqlserverDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1422";
String user="sa";
String password="sa";
Connection con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from book");
System.out.println("isbn"+" "+"code"+" "+"thename");
while(rs.next()) {
int a=rs.getInt("isbn");
int b=rs.getInt("code");
String c=rs.getString("thename");
System.out.println(a+" "+b+" "+c);
}
stmt.close();
con.close();
}
catch (ClassNotFoundException e) {
System.err.println("ClassNotFoundException : "+e.getMessage());
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
为什么我运行了后出现ClassNotFoundException : com.microsoft.jdbc.sqlserver.sqlserverDriver
请问是哪里的毛病,应该怎么处理,请高手指点,我在这里都2个多星期了,快疯掉了。谢谢!初学,希望讲的明白点。
|
缺SQL Server的JDBC驱动程序,到网上找一个,存到某个目录下,再加到classpath里就可以了。
或者使用JDBC-ODBC桥方式访问,就像这样:
public class OdbcTest
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
String url = "jdbc:odbc:myDSN"; //"myDSN":在本机建立的ODBC数据源。
Connection db = DriverManager.getConnection(url,"sa","sa");
Statement stmt_sql = db.createStatement();
String stmt_str = "select * from book";
ResultSet rs = stmt_sql.executeQuery(stmt_str);
while (rs.next())
{
... ...
}
rs.close();
stmt_sql.close();
db.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
} //end main
}
或者使用JDBC-ODBC桥方式访问,就像这样:
public class OdbcTest
{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
String url = "jdbc:odbc:myDSN"; //"myDSN":在本机建立的ODBC数据源。
Connection db = DriverManager.getConnection(url,"sa","sa");
Statement stmt_sql = db.createStatement();
String stmt_str = "select * from book";
ResultSet rs = stmt_sql.executeQuery(stmt_str);
while (rs.next())
{
... ...
}
rs.close();
stmt_sql.close();
db.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
} //end main
}