创建没有返回值的方法,编译出错. 楼上的兄弟,我一改成public void conn(),马上提示:加上return还是不行。DataConnection.java:28: missing return statement { ^DataConnection.java:43: missing return statement { ^2 errors啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 } 你的方法中一定还有return语句,如果声明为void就不能有返回语句,如果有返回语句,就需要声明为返回类型。 唉,各位兄弟啊,你们可能没明白我的意思!!!我已经试过了几个方法:第一:public conn() //负责数据库连接,没有返回值. { try { Class.forName(DBDriver); } catch(java.lang.ClassNotFoundException e) { System.out.println("DBconn():"+e.getMessage()); } }这样编译也会出错.第二: 改成下面这样还是出错.public void conn() 第三:加上RETURN还是出错. 首先说明下 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; } } 弹出窗口,分页 ftp问题!!! 初学者求救!为什么我打不开任何网页? STRUTS2如何实现跳转? Dwr convert的问题 如何将网页的注册信息写入文件,然后在登陆的时候验证这些信息 这个连接池怎么调用才能真正提高性能?怎样避免每次都得初始化?多刷新几次就死机? jsp聊天室的问题 我的request.getParameter("id");传不了! 帮个忙,我想建立一个javabean,其中有一个函数,调用这个方法有4个参数返回.不止如何设计?可否举个例子? 怎样在JSP中对ORACLE中的Date字段进行读写? javascript 能够传递数组参数吗?
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
}
我已经试过了几个方法:
第一:
public conn() //负责数据库连接,没有返回值.
{
try {
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("DBconn():"+e.getMessage());
}
}这样编译也会出错.第二: 改成下面这样还是出错.
public void conn() 第三:加上RETURN还是出错.
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;
}
}