简体   繁体   中英

This code is not showing output after running it in java?

Why is this code is not showing output after running it in Netbeans? It is supposed to ask "Enter username to search" after connecting to the database in mysql. I have created Database and Table in Netbeans 8.0.2 using MySql. All drivers are installed correctly as i can see them in service panel.I am using Netbeans 8.0.2 as i works on webservice. I am not sure if it is JDBC problem or something else, as I can access all Databases and Tables from Netbeans 8.0.2 perfectly. Please help.

public class Passwordchecker{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/checksql", "username", "password");
    Statement stmt = con.createStatement(); 

    char answer;
    do{

    System.out.print("Enter username to search for: ");
    String name = sc.next();

    String SQL = "SELECT * FROM people WHERE name='" + name + "'";

    ResultSet rs = stmt.executeQuery(SQL);

    if(rs.next()) {
        System.out.println("Success!");
    } else {
        System.out.println("Failure!");
    }

    System.out.print("Do you want to search for another name? (Y/N): ");
    answer = sc.next().charAt(0);
    } while(answer == 'Y' || answer == 'y');

    } catch(Exception e) {
        e.printStackTrace();
    }
 }

If you don't get any result is because the code execution is throwing an exception. When the execution reaches the exception part, there isn't more code to execute, so the program is finished and it doesn't show anything on the console.

I guess the statement stmt = con.createStatement(); part is throwing the exception try execute this code:

System.out.print("Posible Failure 1");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/checksql", "username", "password");

System.out.print("Posible Failure 2");

statement stmt = con.createStatement(); 

System.out.print("Posible Failure 3");

And add this line in the exception part:

catch(Exception e) {
    e.printStackTrace();
}

This will tell you what and where exception is throwing.

Make sure the MYSQL service is running and you must have all the imports you need.

There are couple of things you can try to debug the code.

  1. Here hard code a name.

     String name = sc.next(); 

    something like

     String name = "An Existing name in DB"; 
    1. your catch dont have any mechanism to print exception so you will never know what happened. Modify the catch:

       catch(Exception e) { e.printStackTrace(); } 

Do this and let me know we will go from there.

Have you checked your code:
You need these imports

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

In the code you have provided. You have missed "}" in end. Check it.

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