1

I am using XAMPP->Mysql to create database and using Netbeans IDE 8.1 fro create java

My XAMPP Control Panel pic

Student database pic

My Code

//default package
//1st step
import java.sql.*;

public class DemoJDBC {

    public static void main(String[] args) {
        try{
          String Query = "Select * from Student";
          //2nd step  
            Class.forName("com.mysql.jdbc.Driver");
          //3rd step
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "0");
          //4th step
            Statement st = con.createStatement();
          //5th step
            ResultSet rs = st.executeQuery(Query);
            rs.next();
            String name = rs.getString("sname");
            System.out.println(name);

          //6th step
          con.close();
        }
        catch (Exception e){

        }
    }

}

Why it didn't show output name ? It just show "BUILD SUCCESSFUL (total time: 1 second)" in netbeans output

1
  • 1
    "BUILD SUCCESSFUL" indicates you built the code, but maybe you didn't run it? Commented Nov 25, 2016 at 8:38

2 Answers 2

1

You need to loop through the ResultSet to get the tuples or rows. So while looping you retrieve whatever data or field you want to get. try:

    public static void main(String[] args) {
        try {
            String Query = "Select * from Student";
            //2nd step  
            Class.forName("com.mysql.jdbc.Driver");
            //3rd step
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "0");
            //4th step
            Statement st = con.createStatement();
            //5th step
            ResultSet rs = st.executeQuery(Query);

            //Loop to retrieve tuple(s) from the ResultSet rs
            while (rs.next()) {
                String name = rs.getString("sname");
                System.out.println(name);
            }

            //6th step
            con.close();
        } catch (Exception e) {

        }
    }

NOTE if by default you did not change the password of the root user it is just the empty String (thus "" and not "0"). Other than that you know what you are doing.

Sign up to request clarification or add additional context in comments.

9 Comments

It's still show run: BUILD SUCCESSFUL (total time: 1 second)
take note of the NOTE part of my answer, if it would be helpful.
Are you running or you are just building the project? If you are just building, you can run using SHIFT+F6 to run this file only.
Should I change jdbc:mysql://localhost/student to jdbc:mysql://localhost:8080/student ?
Mysql is running on port 3306 according to your xampp so if anything, you rather change it to 3306. I hope you have also added the mysql java driver jar file to libraries.
|
0

Along with the correction of your code to loop through the ResultSet, you also need to correct your connection string as shown below:

Considering that your MySQL is running on the default MySQL port 3306 (which I see that you already are), the connection string needs to be updated.

Also saw that password for the user root is '0', is it really the password?

public static void main(String[] args) {
    try {
        String query = "SELECT * FROM Student";
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "0");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);

        //Loop to retrieve tuple(s) from the ResultSet rs
        while (rs.next()) {
            String name = rs.getString("sname");
            System.out.println(name);
        }
        con.close();
    } catch (Exception e) {
    }
}

Also ensure to have your MySQL Connector/J jar file to be present in your CLASSPATH to not to face any reference issues.

Hope that this helps!

2 Comments

when I change to "jdbc:mysql://localhost:3360/student", "root", ""); , it's worked..!. Didn't it set by default port in MySQL ?
It's by design, you need to mention the port number in your connection string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.