简体   繁体   中英

How to retrieve elements from a database into a JDBC program

I tried a basic program to retrieve data from a database table into a java program. At the end of the compilation, when running the code an exception occurs. No error is shown in the console. It displays the exception message

import java.sql.*;
public class class1 {
    public static void main(String args[]){
        String url = "jdbc:mysql://localhost:3306//orders";
        String username = "root";
        String password = "Luxan@22";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, username, password);
            Statement st = con.createStatement();
            ResultSet rs =  st.executeQuery("SELECT CUST_NAME FROM CUSTOMERS");
            System.out.println("List of Registered customers: ");
            while(rs.next()){
                System.out.println(rs.getString("cust_name"));
            }
            st.close();
            con.close();
        }
        catch(Exception e){
            System.out.println("An Exception error occured while processing JDBC programme");
        }
    }
}

Below is the output got in my console window

Fri Jan 17 19:34:24 IST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

An Exception error occured while processing JDBC programme

The problem is with your URL. Replace

"jdbc:mysql://localhost:3306//orders"

with

"jdbc:mysql://localhost:3306/orders"

Note that I have removed one / before orders .

尝试将?useSSL=false添加到连接 url 的末尾。

It is difficult to see what went wrong, without knowing your table structure. But here is another way of writing your program. Maybe it helps you come closer to your goal:

public class Class1 {

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");   // this line is optional since Java 1.6 Normally for MySQL you shouldn't have to do this
            // This is called "try-with-resources". It means that inside your "try" declaration you
            // tell Java what things you want to open (here you wnat to open a connection)
            // and it will automatically close any connection (on failure or success) for you
            try (
                    Connection connection = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/orders", // before the database name there is only one /
                            "root", // the database user
                            "password"); // the password
                    Statement statement = connection.createStatement() // the same try-with resources can open the statement for your
                    ) {

                ResultSet rs = statement.executeQuery("SELECT CUST_NAME FROM CUSTOMERS");
                while (rs.next()) {
                    System.out.println(rs.getString("cust_name"));
                }

            } catch (SQLException ex) {
                Logger.getLogger(Class1.class).log(Level.SEVERE, null, ex);   // this will properly log your SQLException. Don't be afraid, SQL error messages are scary and Java Exceptions are impressive. But they will tell you where the problem is
            }
        } catch (ClassNotFoundException ex) {   // if you register your driver you need to catch the exception as well
            Logger.getLogger(Class1.class).log(Level.SEVERE, null, ex);
        }
    }
}

You can read more about this example on MkYong .

I hope it helps you.

One more thing:

Class names start with a capital letter: Class1 instead of class1 . Because variable names start with a small letter. Imagine you want to create an instance of a class called car . Then you would say car car = new car(); which is unreadable. Car car = new Car() however is clear :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM