楼上的兄弟,我一改成public void conn(),马上提示:加上return还是不行。DataConnection.java:28: missing return statement
    {
    ^
DataConnection.java:43: missing return statement
    {
    ^
2 errors啊

解决方案 »

  1.   

    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
        }
      

  2.   

    你的方法中一定还有return语句,如果声明为void就不能有返回语句,如果有返回语句,就需要声明为返回类型。
      

  3.   

    唉,各位兄弟啊,你们可能没明白我的意思!!!
    我已经试过了几个方法:
    第一:
    public conn()  //负责数据库连接,没有返回值.
        {     
          try {
               Class.forName(DBDriver);           
              }
          catch(java.lang.ClassNotFoundException e)
              {
                System.out.println("DBconn():"+e.getMessage());
              }      
        }这样编译也会出错.第二: 改成下面这样还是出错.
    public void conn()  第三:加上RETURN还是出错.
      

  4.   

    首先说明下
        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;
               }
            }