How to use java to connect to local mysql?

Do I need to install something? And if necessary, I want to take a complete step to follow. And simple connection coding. Thanks you

+4
source share
6 answers

See this tutorial .

In short:

Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/dbName", "root", "secret"); 

You can also use DriverManager.registerDriver(..) instead of Class.forName(..) .

Of course you need to load mysql-connection-x.jar ( jdbc driver ) and put it in your classpath.

+12
source

First you need to download MySql Driver and add it to your java library or class path. Then try this,

  try { Class.forName("com.MySql.jdbc.Driver"); String Url = "jdbc:MySql://localhost:3306/databaseName"; connection=DriverManager.getConnection(Url); String query="Select * from TableName"; resultSet=statement.executeQuery(query); }catch(Exception ex){ System.out.println("Connection Error"); } 
+4
source

The first line underlines "pagkage", please let me know to solve this problem?

package com.stardeveloper.example;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

0
source

I would call it:

http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

He will have all the examples needed to connect to MySQL using Connector-J.

0
source

Class.forName ("com.MySql.jdbc.Driver");

Is β€œdriver” using the next step to figure it out?

β€’ Click "Control Panel" in Windows. β€’ Click "Administration" β€’ Click "Data Sources" (ODBC) β€’ In the form that appears, select Add. β€’ In the following form, select a driver from the list β€” for example, SQL Server

0
source

You can try using spring data source. http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/jdbc/datasource/package-summary.html

I found it convenient. Also, if this is a large-scale application, consider pooling as well.

0
source

Source: https://habr.com/ru/post/1306686/


All Articles