0

I wrote this java program to fetch data from database table.I solved all the errors and exceptions regarding the path and environment variables but at the end the values present in my mysql database table are not getting displayed.

Java code:

import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.cj.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/sonoo","root","Password@123");  
//here sonoo is database name, root is username and password  
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getInt(3));  
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  
}

Mysql table:-

create database sonoo;  
use sonoo;
create table emp(id int,name varchar(40),age int);

I am expecting this code to fetch records from my database table

lonewolf
  • 1
  • 1
  • 3
    ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Apr 05 '23 at 14:12
  • 3
    what errors did you get? Is it running but not showing any errors? – experiment unit 1998X Apr 05 '23 at 14:16
  • 5
    *I am expecting this code to fetch records from my database table* Did you *put* any data in it? You didn't show that happening – g00se Apr 05 '23 at 14:17
  • Step through your code with the debugger. – tgdavies Apr 06 '23 at 00:28
  • } catch(SQLException e) { You can use getString on the primitives if they are e.g. int. Since jdk6 and jdbc4.0 any JDBC drivers found on the classpath are loaded automatically, versions before jdbc4.0 must be loaded manually. When loading manually wrap the class.forname in a ClassNotFound exception. Sorry "my bad" the url is ok for what is there. – Samuel Marchant Apr 06 '23 at 01:17

1 Answers1

-1

Technically a result set should immediately be transferred onto a multi dimensional array first if it can all be human readable String.

    try{
while(rs.next()){  
    System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getInt(3)); 
    }// put delimiters on the while
    // then 
// close results and statement here also
    con.close();
} catch(SQLException e) { 
e.prinStackTrace();
}

It should also be wrapped in a SQLException. You can use getString on the primitives if they are e.g. int. Since jdk6 and jdbc4.0 any JDBC drivers found on the classpath are loaded automatically, versions before jdbc4.0 must be loaded manually. When loading manually wrap the class.forname in a ClassNotFoundException then you follow it with the "try" with the SQLException Closing JDBC SQL connections. https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts839085.html#:~:text=To%20close%20a%20Statement%20%2C%20ResultSet,before%20you%20close%20the%20connection

Samuel Marchant
  • 331
  • 2
  • 6
  • 1
    "a result set should immediately be transferred onto a multi dimensional array" Why? – tgdavies Apr 06 '23 at 04:07
  • I remember the tutorial from years back saying that, the statement and result set should be closed after obtaining the result set, after it is closed it cannot be accessed. So "obtain it all" do the required close operations and the thread process can continue unblocked. It's a little like opening a file and reading it then closing metaphorically. – Samuel Marchant Apr 07 '23 at 04:33
  • A Java connector is an intermediary program (has its own main method) to execute a command on another piece of software such as a database, it has a main method, you must switch the software connector off after or the whole program will sit in RAM unattached unretrievable, unless it is a connection pool management for such connections(retrievable). – Samuel Marchant Apr 07 '23 at 04:42
  • ...and close the result set after reading it into what sensibly holds it. Requerying a result set is usually done on the database side a select statement being resultsets is a variable=. and the variable being queried setting up the initial answer columns with a name using the sql keyword "as" (sub query) into another variable as the final result set to obtain 1 column if it uses comparison to 2 resulting columns "as" from the first query. – Samuel Marchant Apr 07 '23 at 05:10