当前位置: 技术问答>java相关
用JDBC连接数据库问题
来源: 互联网 发布时间:2015-07-24
本文导语: 我照书超了一段程序,运行时显示ClassNotFoundException:Could not lacate driver。 我用DATABASE PILOT是好的,能看到INFORMIX数据库。 源码如下: import java.sql.*; public class createTable{ public static void main(String args[]){ Con...
我照书超了一段程序,运行时显示ClassNotFoundException:Could not lacate driver。
我用DATABASE PILOT是好的,能看到INFORMIX数据库。
源码如下:
import java.sql.*;
public class createTable{
public static void main(String args[]){
Connection con=null;
try{
Class.forName("com.informix.jdbc.ifxDriver").newInstance();
System.out.println("JDBC driver loaded");
con=DriverManager.getConnection("jdbc:informix-sqli://fdbser:1024/fdb:INFORMIXSERVER=server;user=fdb;password=qwe123");
System.out.println("Database connection established");
Statement stmt=con.createStatement();
String upd="CREATE TABLE Author (Author_ID Integer NOT null ); ";
stmt.executeUpdate(upd);
System.out.println("Table AUthor created");
upd="CREATE TABLE Author1 (Author_ID Integer NOT null );";
stmt.executeUpdate(upd);
System.out.println("Table AUthor1 created");
upd="CREATE TABLE Author2 (Author_ID Integer NOT null );";
stmt.executeUpdate(upd);
System.out.println("Table AUthor2 created");
}catch (ClassNotFoundException cnfe){
System.out.println("ClassNotFoundException:Could not lacate driver");
}catch (SQLException cnfe) {
System.out.println("SQLException:"+cnfe);
}catch (Exception e) {
System.out.println("An unknown error occured while connecting to the database");
}finally{
try{
if(con!=null)
con.close();
}catch (SQLException sqle){
System.out.println("Unable to close database connection.");
}
}
}
}
我用DATABASE PILOT是好的,能看到INFORMIX数据库。
源码如下:
import java.sql.*;
public class createTable{
public static void main(String args[]){
Connection con=null;
try{
Class.forName("com.informix.jdbc.ifxDriver").newInstance();
System.out.println("JDBC driver loaded");
con=DriverManager.getConnection("jdbc:informix-sqli://fdbser:1024/fdb:INFORMIXSERVER=server;user=fdb;password=qwe123");
System.out.println("Database connection established");
Statement stmt=con.createStatement();
String upd="CREATE TABLE Author (Author_ID Integer NOT null ); ";
stmt.executeUpdate(upd);
System.out.println("Table AUthor created");
upd="CREATE TABLE Author1 (Author_ID Integer NOT null );";
stmt.executeUpdate(upd);
System.out.println("Table AUthor1 created");
upd="CREATE TABLE Author2 (Author_ID Integer NOT null );";
stmt.executeUpdate(upd);
System.out.println("Table AUthor2 created");
}catch (ClassNotFoundException cnfe){
System.out.println("ClassNotFoundException:Could not lacate driver");
}catch (SQLException cnfe) {
System.out.println("SQLException:"+cnfe);
}catch (Exception e) {
System.out.println("An unknown error occured while connecting to the database");
}finally{
try{
if(con!=null)
con.close();
}catch (SQLException sqle){
System.out.println("Unable to close database connection.");
}
}
}
}
|
Class.forName("com.informix.jdbc.ifxDriver").newInstance(); --有问题
可能是没有安装数据库驱动程序
可能是没有安装数据库驱动程序
|
你是没把那个驱动com.informix.jdbc.ifxDriver类设置到环境变量里去吧?