当前位置: 技术问答>java相关
这段连接数据库的代码为什么会有SQLException异常?
来源: 互联网 发布时间:2015-10-27
本文导语: import java.sql.*; public class MakingAConnection{ public static void main(String[] arg){ try{ Class.forName("COM.cloudscape.core.JDBCDriver"); String sourceURL=new String("jdbc:cloudscape:../database/Wrox4370DB"); Connection databaseConnection=DriverManag...
import java.sql.*;
public class MakingAConnection{
public static void main(String[] arg){
try{
Class.forName("COM.cloudscape.core.JDBCDriver");
String sourceURL=new String("jdbc:cloudscape:../database/Wrox4370DB");
Connection databaseConnection=DriverManager.getConnection(sourceURL);
//System.out.println("Successfully connected!!");
Statement statement=databaseConnection.createStatement();
ResultSet artistNames=statement.executeQuery("SELECT artistid,artistname "+
"FROM artistsandperformers");
while (artistNames.next())
{
System.out.println(artistNames.getInt("artistid")+" "+
artistNames.getString("artistname"));
databaseConnection.close();
}
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch (SQLException sqle)
{
System.err.println(sqle);
}
}
}
下面是解译:
H:begjavadbMakingAConnection>javac MakingAConnection.java
H:begjavadbMakingAConnection>java MakingAConnection
This copy of Cloudscape is licensed for DEVELOPMENT ONLY.
It is a violation of the license agreement to deploy this version in a productio
n application.
For information about licensing Cloudscape for application deployment,
contact cloud-sales@informix.com or call 888/595-2821 ext. 7664.
Additional licensing information can be found at
http://www.cloudscape.com/licensing.
Successfully connected!!
100 10,000 Maniacs
SQL Exception: No current connection.
public class MakingAConnection{
public static void main(String[] arg){
try{
Class.forName("COM.cloudscape.core.JDBCDriver");
String sourceURL=new String("jdbc:cloudscape:../database/Wrox4370DB");
Connection databaseConnection=DriverManager.getConnection(sourceURL);
//System.out.println("Successfully connected!!");
Statement statement=databaseConnection.createStatement();
ResultSet artistNames=statement.executeQuery("SELECT artistid,artistname "+
"FROM artistsandperformers");
while (artistNames.next())
{
System.out.println(artistNames.getInt("artistid")+" "+
artistNames.getString("artistname"));
databaseConnection.close();
}
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch (SQLException sqle)
{
System.err.println(sqle);
}
}
}
下面是解译:
H:begjavadbMakingAConnection>javac MakingAConnection.java
H:begjavadbMakingAConnection>java MakingAConnection
This copy of Cloudscape is licensed for DEVELOPMENT ONLY.
It is a violation of the license agreement to deploy this version in a productio
n application.
For information about licensing Cloudscape for application deployment,
contact cloud-sales@informix.com or call 888/595-2821 ext. 7664.
Additional licensing information can be found at
http://www.cloudscape.com/licensing.
Successfully connected!!
100 10,000 Maniacs
SQL Exception: No current connection.
|
databaseConnection.close();
把这句去掉。
把这句去掉。