Connect Java to a MySQL Database

Connect Java to a MySQL database

DriverManager is a fairly old way of doing things. The better way is to get a DataSource, either by looking one up that your app server container already configured for you:

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");

or instantiating and configuring one from your database driver directly:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("scott");
dataSource.setPassword("tiger");
dataSource.setServerName("myDBHost.example.org");

and then obtain connections from it, same as above:

Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS");
...
rs.close();
stmt.close();
conn.close();

Connecting to MySQL DB in Java

  1. Check whether your MySQL database server is hosted/up/on.
  2. Check if the username and password entered of the connecting database is correct.
  3. Check if you have include the MySQL jar file in your build path and lib folder of your web server e.g. Tomcat
  4. Check the URL of the connecting database is correct.

Also do this changes and see

String url = "jdbc:mysql://mysql2.000webhost.com/";

Also I found this

MySQL from 000webhost doesn't allow you to connect from external applications, just from within pages hosted in their domain.

Please check: How can I connect to MySQL from my computer?

Connect Java remotely to MYSQL DB on another network

If you are trying to connect to remote database then yo need to change the database url from localhost to remote server ip address.

jdbc:mysql://Atomic-PC:3306/test

to

jdbc:mysql://<db-server-ip-address>:<db-server-port>/<db-name>

Assuming, remote server ip address is 10.234.05.123 and database port number is 3300 and database name is remoteDB. Then,

jdbc:mysql://10.234.05.123:3300/remoteDB

Cannot connect Java to MySQL database even after having jar file

Mentioned serverTimezone explicitly in the code to fix the issue.

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdatabase?serverTimezone=UTC", "root", "MyPassword");

Why I can't connect MySQL database to Simple Java program ? Using updated versions of all softwares

I found the Answer of this. This is problem of Incompatible driver. I have read documentations from official site of MYSQL. It Explains this.
There are two series of JDBC Pure Java Drivers by MYSQL.

1.Connector J 8. Series - This drive driver adds many extra facilities, but most of the times on small basic programs it cant work on its own. It needs core driver 5.0 installed.

2.Connector J 5. Series - This looked like most compatible driver series. Its original driver you need to install. So if check if you are using correct driver.

Your 5.0 Driver will be present in C:\Program Files (x86)\MySQL\Connector J 5.1\mysql-connector-java-5.1.48.jar by default. Create CLASSPATH Variable, give location to this driver and your .class file of program. Now it will work.

EDITS : I previously said J7 Driver is Add-on to J5 driver. I was wrong, Someone corrected me in comments. So I have reedited this post. I cant guarantee my above described answer true, cause am learning myself. I just tried to tell how I got my solution.



Related Topics



Leave a reply



Submit