Akshay Gupta
Akshay Gupta

Reputation: 361

Connecting to MySQL database using Java in command prompt

Since I don't have netbeans right now, I am trying to connect to MySQL database from my Java code through command prompt. But it it is not taking the mysql-coonectivity.jar file.

Does anyone know any way to run my program??? Please help.

Upvotes: 0

Views: 10005

Answers (3)

maruf
maruf

Reputation: 669

To execute program in Linux (Ubuntu) OS follow the below command format:

java -cp .:/usr/share/java/mysql-connector-java-8.0.25.jar MainProgram

Note:

  1. Assuming you are in the directory where MainProgram.class is exist
  2. After .(dot -- this is for current directory) there is :(colon) as a separator in Linux

Upvotes: 0

SparkOn
SparkOn

Reputation: 8956

Try executing the program as it is windows OS

java -cp .;path\of\your\mysql-connector-java-5.1.6.jar className

In case you don't have the mysql-connector-java-[version].jar get it from here

Upvotes: 4

gprathour
gprathour

Reputation: 15333

You need to add your MySQL connector jar file while compiling and running your program, you can do it the following way,

To compile :

javac -g -cp mysql-coonectivity.jar; YourFileName.java

To Run

java -cp mysql-coonectivity.jar; YourMainClass

NOTE: The above written syntax assumes that your jar file is present at same location as of your program.

Upvotes: 3

Related Questions