当前位置: 技术问答>java相关
为什么编译没有通过?
来源: 互联网 发布时间:2017-04-16
本文导语: import java.sql.*; class test { public static void main(String[] args) { java.sql.Connection sqlCon; //数据库连接对象 java.sql.Statement sqlStmt; //SQL语句对象 java.sql.ResultSet sqlRst; //结果集对象 java.lang.String strCon; //数据...
import java.sql.*;
class test
{
public static void main(String[] args)
{
java.sql.Connection sqlCon; //数据库连接对象
java.sql.Statement sqlStmt; //SQL语句对象
java.sql.ResultSet sqlRst; //结果集对象
java.lang.String strCon; //数据库连接字符串
java.lang.String strSQL; //SQL语句
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strCon = "jdbc:odbc:wind";
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
sqlStmt = sqlCon.createStatement();
strSQL = "select name,email,body from [Test_DB].[dbo].[guestbook]";
sqlRst = sqlStmt.executeQuery(strSQL);
while(sqlRst.next()){
System.out.println(sqlRst.getString(1));
}
}
}
出错信息
E:javatest.java:12: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
^
E:javatest.java:14: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
^
E:javatest.java:15: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlStmt = sqlCon.createStatement();
^
E:javatest.java:17: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlRst = sqlStmt.executeQuery(strSQL);
^
E:javatest.java:18: unreported exception java.sql.SQLException; must be caught or declared to be thrown
while(sqlRst.next()){
^
E:javatest.java:19: unreported exception java.sql.SQLException; must be caught or declared to be thrown
System.out.println(sqlRst.getString(1));
^
6 errors
输出完成 (耗时 1 秒) - 正常终止
class test
{
public static void main(String[] args)
{
java.sql.Connection sqlCon; //数据库连接对象
java.sql.Statement sqlStmt; //SQL语句对象
java.sql.ResultSet sqlRst; //结果集对象
java.lang.String strCon; //数据库连接字符串
java.lang.String strSQL; //SQL语句
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strCon = "jdbc:odbc:wind";
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
sqlStmt = sqlCon.createStatement();
strSQL = "select name,email,body from [Test_DB].[dbo].[guestbook]";
sqlRst = sqlStmt.executeQuery(strSQL);
while(sqlRst.next()){
System.out.println(sqlRst.getString(1));
}
}
}
出错信息
E:javatest.java:12: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
^
E:javatest.java:14: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
^
E:javatest.java:15: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlStmt = sqlCon.createStatement();
^
E:javatest.java:17: unreported exception java.sql.SQLException; must be caught or declared to be thrown
sqlRst = sqlStmt.executeQuery(strSQL);
^
E:javatest.java:18: unreported exception java.sql.SQLException; must be caught or declared to be thrown
while(sqlRst.next()){
^
E:javatest.java:19: unreported exception java.sql.SQLException; must be caught or declared to be thrown
System.out.println(sqlRst.getString(1));
^
6 errors
输出完成 (耗时 1 秒) - 正常终止
|
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strCon = "jdbc:odbc:wind";
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
sqlStmt = sqlCon.createStatement();
strSQL = "select name,email,body from [Test_DB].[dbo].[guestbook]";
sqlRst = sqlStmt.executeQuery(strSQL);
while(sqlRst.next()){
System.out.println(sqlRst.getString(1));
}
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strCon = "jdbc:odbc:wind";
sqlCon = java.sql.DriverManager.getConnection(strCon,"root","123");
sqlStmt = sqlCon.createStatement();
strSQL = "select name,email,body from [Test_DB].[dbo].[guestbook]";
sqlRst = sqlStmt.executeQuery(strSQL);
while(sqlRst.next()){
System.out.println(sqlRst.getString(1));
}
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。