当前位置: 技术问答>java相关
jdbc的配置???
来源: 互联网 发布时间:2014-12-22
本文导语: 我有一个数据库,叫jsk,在ODBC中的数据源名称叫dbname,中间有一个表叫myTable, 如下的程序编译通过,执行时却提示EXCEPTION:No suitable driver.为什么? import java.net.URL; import java.sql.*; public class QueryMyTable{ public static void...
我有一个数据库,叫jsk,在ODBC中的数据源名称叫dbname,中间有一个表叫myTable,
如下的程序编译通过,执行时却提示EXCEPTION:No suitable driver.为什么?
import java.net.URL;
import java.sql.*;
public class QueryMyTable{
public static void main(String args[]){
try{
//connect to the database
String theUrl="jdbc:odbc:dbname";
Connection dbConn=DriverManager.getConnection(theUrl,"sa","");
//execute the SELECT statement
Statement sqlStmt=dbConn.createStatement();
ResultSet rSet=sqlStmt.executeQuery("SELECT xInt,yString FROM myTable");
//iterate through the result rows and print out the values obtained
System.out.println("Return results");
while(rSet.next()){
int xIntVal=rSet.getInt("xInt");
String yStrVal=rSet.getString("yStr");
System.out.print("xIntVal="+xIntVal);
System.out.print("yStrVal="+yStrVal);
System.out.print("/n");
}
sqlStmt.close();
dbConn.close();
}
catch(Exception e){
System.out.println("EXCEPTION:"+e.getMessage());
}
}
}
如下的程序编译通过,执行时却提示EXCEPTION:No suitable driver.为什么?
import java.net.URL;
import java.sql.*;
public class QueryMyTable{
public static void main(String args[]){
try{
//connect to the database
String theUrl="jdbc:odbc:dbname";
Connection dbConn=DriverManager.getConnection(theUrl,"sa","");
//execute the SELECT statement
Statement sqlStmt=dbConn.createStatement();
ResultSet rSet=sqlStmt.executeQuery("SELECT xInt,yString FROM myTable");
//iterate through the result rows and print out the values obtained
System.out.println("Return results");
while(rSet.next()){
int xIntVal=rSet.getInt("xInt");
String yStrVal=rSet.getString("yStr");
System.out.print("xIntVal="+xIntVal);
System.out.print("yStrVal="+yStrVal);
System.out.print("/n");
}
sqlStmt.close();
dbConn.close();
}
catch(Exception e){
System.out.println("EXCEPTION:"+e.getMessage());
}
}
}
|
先要load driver嘛。
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFound: ");
System.err.println(e.getMessage());
}
//然后再建立connection
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFound: ");
System.err.println(e.getMessage());
}
//然后再建立connection