Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:58:24+00:00 2026-06-14T16:58:24+00:00

I have the next code. I save it in the file: ‘MysqlConnect.java’. import java.sql.*;

  • 0

I have the next code. I save it in the file: ‘MysqlConnect.java’.

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "root";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);
        System.out.println("Connected to the database");
        conn.close();
        System.out.println("Disconnected from database");
    } catch (Exception e) {
        e.printStackTrace(); 
    }
  }
}

I know how to run it:

javac MysqlConnect.java
java MysqlConnect

I have some questions:

1) where should I save this file?
(I have a folder of: mysql-connector-java-5.1.22).

2) I created a database and a table in mysql. I don’t need to insert that into the code? for example:

String dbname = "database_alon";

3) If the answer of the second question is ‘YES’, I only have a password to my database, not an username. so what should I insert to the parameter of userName?

so, I think that I have to change the code to:

String dbName = "database_alon";
String driver = "com.mysql.jdbc.Driver";
String userName = ""; 
String password = "ADMINALON";

right? and what about the question 1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:58:25+00:00Added an answer on June 14, 2026 at 4:58 pm

    You can have your MysqlConnect.java file wherever you want. You should just add the folder containg the MysqlConnect.class in your classpath. Moreover, in order to run properly you should also add the mysql-connector-java-5.1.22.jar to the classpath in order to be able to find the classes you require for running your class.

    In order to avoid mutiple additions in the classpath you could add the mysql-connector-java-5.1.22.jar in the folder containing the
    MysqlConnect.class and make only one update of the classpath.

    As for your second question YES you need to change the code and add the

    String dbname="database_alon"
    

    instead of

    String dbName = "jdbctutorial";
    

    I dont think you dont have a username in you database because it picks the root username by default. Try root for username and tell me what it gives you but make sure to update your classpath as stated above. Make also sure you are using the default port 3306 for connecting to the database.


    P.S. What program you use for managing you databases? I can provide more help knowing that.

    P.S.S You could use an IDE such as Eclipse or Netbeans and that could make the deployment much more easier. You will not need to touch the classpaths and other variables because the IDE will take care by just selecting the jars you want to use for your project.

    P.S.S If you dont know how to add a jar in your classpath please provide your operating system in order to provide help for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next code: class Printer{ Activity activity; public Printer (Activity activity) {
I have the next code : public class AddPrinter extends ListActivity { @Override public
I have written this code: public void reloadDefConfig(File filepath, String fileInJar) { File configFile
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have the next code to retrieve some data from a xml file. The
I have this code in the login.php file : //save user data $token=$user->getId().'_'.hash('sha256',$user->getUsername().microtime()); $name='fsusr_'.$user->getUsername();
I have next code, <form id=form1 runat=server> <asp:Label runat=server ID=Label1 EnableViewState=false /> <asp:CheckBox runat=server
I have next code: int main() { OwnSelect(23, FD_READ | FD_WRITE); // <---- Several
I have the next code in javascript. I deleted some unnecessary items because it
I have the next code: Process p = Runtime.getRuntime().exec(args); and I want my program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.