创建没有返回值的方法,编译出错.
创建没有返回值的方法,编译出错.
我定义了一个方法如下:
public conn() //负责数据库连接,没有返回值.
{
try {
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("DBconn():"+e.getMessage());
}
}
可是在编译时出错
DataConnection.java:16: invalid method declaration; return type required
怎么办?
问题点数:50、回复次数:6Top
1 楼zsh99(白粥先生)回复于 2003-09-01 12:57:48 得分 10
public void conn()Top
2 楼Jamesczh(James)回复于 2003-09-01 13:05:21 得分 0
楼上的兄弟,我一改成public void conn(),马上提示:加上return还是不行。
DataConnection.java:28: missing return statement
{
^
DataConnection.java:43: missing return statement
{
^
2 errors啊Top
3 楼Keepers(中文昵称)回复于 2003-09-01 13:08:00 得分 10
Connection connection = null;
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "mydatabase";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
Top
4 楼wangwd(coffee I love)回复于 2003-09-01 13:10:36 得分 30
你的方法中一定还有return语句,如果声明为void就不能有返回语句,如果有返回语句,就需要声明为返回类型。Top
5 楼Jamesczh(James)回复于 2003-09-01 13:13:59 得分 0
唉,各位兄弟啊,你们可能没明白我的意思!!!
我已经试过了几个方法:
第一:
public conn() //负责数据库连接,没有返回值.
{
try {
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("DBconn():"+e.getMessage());
}
}
这样编译也会出错.
第二: 改成下面这样还是出错.
public void conn()
第三:加上RETURN还是出错.Top
6 楼tanbo(韦小宝)回复于 2003-09-01 13:24:15 得分 0
首先说明下
public conn()是构造函数,用以初始化的,无返回值
public void conn() 是不可以有返回值的
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class Dconn{
Connection conn=null;
Statement stmt=null;
public Dconn()
{
try{
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:/OracleDS");
conn=ds.getConnection();
}
catch(SQLException e){
System.out.print("e0"+e);
}
catch(NamingException e)
{
System.out.print("e1"+e);
}
}
public Statement getStatement(){
try{
stmt=conn.createStatement();
return stmt;
}
catch(SQLException e)
{
System.out.println(e);
}
return null;
}
}
Top



