请问这样的错误什么意思?涉及数据库的,含全部代码

netsfy 2004-05-01 10:25:01
编译出错信息:
sql_data.java:10: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
Connection conn= DriverManager.getConnection(url,user,password);
^
1 error
请按任意键继续. . .


代码如下

//sql_data.java
package news;
import java.sql.*;
public class sql_data{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://10.5.67.232:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="winefox";
Connection conn= DriverManager.getConnection(url,user,password);

Statement stmt = null;
ResultSet rs = null;

public sql_data()
{
try
{Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("sql_data(): " + e.getMessage());
}
}

public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{System.err.println("sql_data.executeUpdate:"+ex.getMessage());
}
}

public ResultSet executeQuery(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);

}
catch(SQLException ex)
{
System.err.println("sql_data.executeQuery:"+ex.getMessage());
}
return rs;
}

public void executeUpdate(String sql)
{
try {
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}

public void executeDelete(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("sql_data.executeDelete:"+ex.getMessage());
}
}

public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}



}
...全文
74 10 点赞 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mars_lzg 2004-05-02
sql_data();
其他execute方法都要加这句!
  • 打赏
  • 举报
回复
mars_lzg 2004-05-02
executeQuery()方法错了,改为如下:
public ResultSet executeQuery(String sql)
{
sql_data();//少了这句
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);

}
catch(SQLException ex)
{
System.err.println("sql_data.executeQuery:"+ex.getMessage());
}
return rs;
}
  • 打赏
  • 举报
回复
boy 2004-05-02
估计是没连上数据库
访问数据库的语句必须有例外处理,就是要写在
try{
}catch(){}
中,你的Connection conn= DriverManager.getConnection(url,user,password); 没有这样做
  • 打赏
  • 举报
回复
allenhe 2004-05-02
DriverManager.getConnection(url,user,password);
抛出SQLException


enjoy~~
btw:用一个ide会少掉很多这种无谓的时间
  • 打赏
  • 举报
回复
netsfy 2004-05-02
请高手指点
  • 打赏
  • 举报
回复
showerXP 2004-05-02
Connection conn= DriverManager.getConnection(url,user,password);
设计就是要在try{}catch{}中捕获异常,你可以看关于getConnection方法的申明。

Connection conn= null;
放在try{}catch{}外先申明就是怕try{}catch{}中申明后会自动销毁变量conn。
  • 打赏
  • 举报
回复
pengfeitian 2004-05-02
gzing
  • 打赏
  • 举报
回复
netsfy 2004-05-01
照着这样是可以编译通过了
Connection conn= DriverManager.getConnection(url,user,password);
改为
Connection conn= null;
但为什么了?为了声明?

但是我在JSP程序中调用的时候这样错误
The server encountered an internal error () that prevented it from fulfilling this request.


java.lang.NullPointerException

JSP程序如下:

<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="sqlbean" scope="page" class="news.sql_data"/>
<font color="navy" face="Arial">
<h2 align="center"><br>
</font><font color="#000000" face="Arial" size="4"><strong>Add New Data</strong></font><font color="navy"></h2>
</font>
<form method="post" action="insert.jsp">
<font face="arial" size="2" color="#000080"><div align="center">
<center>
<p>
<b>类目:</b>
<% String sql,classname;
sql = "select * from class";
ResultSet rs = sqlbean.executeQuery(sql);
out.print("<SELECT name='class'>");
int i=0;
while(rs.next())
{
i++;
classname = (String)rs.getString("class");
out.print("<OPTION value='");
out.print(classname);
out.print("'>");
out.print(classname);
out.print("</OPTION>");
}
rs.close();
out.print("</SELECT>");
%>
  • 打赏
  • 举报
回复
evilzydar 2004-05-01
Connection conn= DriverManager.getConnection(url,user,password);

must be caught or declared to be thrown

try!



  • 打赏
  • 举报
回复
boy 2004-05-01
Connection conn= DriverManager.getConnection(url,user,password);
改为
Connection conn= null;
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-05-01 10:25
社区公告
暂无公告