Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JDBC代码连接我的登录页时出错_Java_Swing_Jdbc - Fatal编程技术网

Java 使用JDBC代码连接我的登录页时出错

Java 使用JDBC代码连接我的登录页时出错,java,swing,jdbc,Java,Swing,Jdbc,这是我连接到oracle 11g数据库的代码,它是成功的。下面的代码用于我的登录页面,并给我一个错误:类型不匹配不能从布尔更改为连接 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Conn{ Connection connection = null; String driverName ="oracle.jdbc.dri

这是我连接到oracle 11g数据库的代码,它是成功的。下面的代码用于我的登录页面,并给我一个错误:类型不匹配不能从布尔更改为连接

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

我还拥有项目所需的所有jar文件。

您的dbConnector()方法返回布尔值而不是连接,它应该返回连接对象。另外,您试图通过类名调用dbConnector()not static方法

您的dbConnector()方法返回布尔值not Connection,它应该返回Connection对象。而且您正试图按类名调用dbConnector()非静态方法

您的期望与实际情况不符,您正在调用的方法声明为返回
布尔值

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}**
    }
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;
}
而且它似乎总是返回
false

要使它满足您的期望,您需要重写它,使其看起来更像

public boolean dbConnector(){

现在,就我个人而言,若不是
ClassNotFoundException
,我至少会让该方法抛出一个
SQLException
,但您可以在构造函数中测试它……处理异常不是该方法的责任,它应该将责任向上传递到调用链上

如果您的期望与实际不符,您调用的方法将声明为返回
布尔值

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}**
    }
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;
}
而且它似乎总是返回
false

要使它满足您的期望,您需要重写它,使其看起来更像

public boolean dbConnector(){

现在,就我个人而言,若不是
ClassNotFoundException
,我至少会让该方法抛出一个
SQLException
,但您可以在构造函数中测试它……处理异常不是该方法的责任,它应该在
Conn
class return
Connection
而不是
Boolean
中将责任传递到调用链上

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}**
    }
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;
}

Conn
类中返回
Connection
而不是
Boolean

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}**
    }
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;
}

错误信息是?(介意从代码中删除外线吗?)?(介意从代码中删除外线吗?)。OracleSql(-transition{})错误:用法不正确。OracleSql(-transition{})