1
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Conn{
    Connection connection = null;
    String  driverName ="oracle.jdbc.driver.OracleDriver"; // for Oracle
    // String driverName = “com.mysql.jdbc.Driver”; //for MySql
    String serverName = "localhost"; // Use this server.
    String portNumber = "1521";
    String sid = "orcl";
    String url="jdbc:oracle:thin:@"+serverName+":"+ portNumber+":"+sid; // for Oracle
    //uri =”jdbc:mysql://server ip or address:port/database name”; //for Mysql
    String username = "scott"; // You should modify this.
    String password = "tiger"; // You should modify this.

    public Conn() {}
    public boolean dbConnector(){
        try {
            // Load the JDBC driver
             Class.forName(driverName);
            // Create a connection to the database
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            // Could not find the database driver
            System.out.println("ClassNotFoundException : "+e.getMessage());
            return false;
        } catch (SQLException e) {
            // Could not connect to the database
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args){
        Conn con =new Conn();
       System.out.println("Connection : " +con.dbConnector());
    }     
}

That's my code for connecting to my oracle 11g database and it is successful. The below code is for my login page and gives me an error: type mismatch cannot change from boolean to Connection

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.JOptionPane;

public class Login {
    private JFrame frame;
    private JTextField textField;
    private JPasswordField passwordField;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Connection connection=null;
    /**
     * Create the application.
     */
    public Login() {
        initialize();
        connection=Conn.dbConnector();  **{This is where i get the error}**
    }

I also have all the jar files required for the project.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

3 Answers3

2

Your expectations don't match realty, the method you are calling is declared as returning boolean

public boolean dbConnector(){

And it always seems to return false

To make it meet your expectations, you would need to rewrite to look more like...

public Connection dbConnector(){

    try {
        // Load the JDBC driver
        Class.forName(driverName);
        // Create a connection to the database
        connection = DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
        // Could not find the database driver
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return connection;
}

Now, personally, I would have the method throw, at least, a SQLException if not the ClassNotFoundException as well...but you could test that in the constructor instead...It's not the responsibility of the this method to deal with the exceptions, it should pass the responsibility up the call chain

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

In Conn class return Connection instead of Boolean

public Connection dbConnector() {
    try {
        // Load the JDBC driver
        Class.forName(driverName);
        // Create a connection to the database
        connection = DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
        // Could not find the database driver
        System.out.println("ClassNotFoundException : " + e.getMessage());
    } catch (SQLException e) {
        // Could not connect to the database
        System.out.println(e.getMessage());
    }
    return connection;
}
ashokramcse
  • 2,841
  • 2
  • 19
  • 41
0

Your dbConnector() method returns boolean not Connection, it should return Connection object. And also you are trying to call dbConnector() not static method by class name

Jignesh Dhua
  • 1,471
  • 1
  • 14
  • 31