Jump to content

Java JDBC programme problem in execution

This is the main code

import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
public class jdbc {
   public static void main(String args[]){
    ResultSet selectquery;
    Statement st;
    try{
        // DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
        // Class.forName("com.mysql.jdbc.Driver");
        Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/Studentname?useSSL=false", "souranil", "soura21nil29");
        System.out.println("Database connected.");
        st=con1.createStatement();
        // st.execute("Use Studentname");
        System.out.printf("Database connnection established.\n");
        st.execute("Insert into Department values(6,'B.Pharama')");
        selectquery=st.executeQuery("Select distinct(deptid),deptname from Department ");
        while(selectquery.next()){
            System.out.println("Department ID: "+selectquery.getString("deptid")+" Departmentname: "+selectquery.getString("deptname"));
        }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
    finally{
        System.out.printf("The programme terminates here.\n");
    }
   }
}

i have this logic and.so when i try to manually compile and execute the program from the terminal it gives this error.image.png.06b0b5b4b855ce9bfd4473f235c2df90.png

 

But when i tried to execute the program through the run button in vscode just above the main function image.png.32b188b3fe3ef7026d578406da91ef6c.png

it worked perfectly as expected and this is the output:

image.png.f65f4409982a579ad537adb50c9aaea0.png

 

Can anyone help me what is the problem here

Link to comment
Share on other sites

Link to post
Share on other sites

Try

javac jdbc.java -classpath "<path to mysql-connector-java-5.1.49.jar>"
java jdbc

Adding things to the global CLASSPATH is asking for trouble. A relative path can throw things off, or if something changes the classpath, all of a sudden other things start breaking.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Souranil21 chakraborty said:

it worked perfectly as expected and this is the output:

  1. First letter of your class name should not be in lower case. (this won't affect your program, but do try to follow naming conventions)
  2. Close the connection once you are done with it, same goes for the statement and result set.
  3. That finally does nothing beside print, inside of that you should be doing if not null rs, st, conn --> close them.
    Or use try-with-resources instead.
     
2 hours ago, Souranil21 chakraborty said:

Can anyone help me what is the problem here

Learn about proper build tools such as Maven or Gradle.

VGhlIHF1aWV0ZXIgeW91IGJlY29tZSwgdGhlIG1vcmUgeW91IGFyZSBhYmxlIHRvIGhlYXIu

^ not a crypto wallet

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×