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

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();
}
}



}
...全文
108 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
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;

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧