Get the App
SLTechnology News&Howtos  ›  Database  › 

Centos6.5 solved the problem that Mysql-connector could not find

Shulou Source: shulou.com Published: 2022-06-01 05:33:33 09月19日 Update

To learn linux, you inevitably need to install the database, but you can't just install the data. If you want to operate the data, you also need a database driver. Many children's shoes who are new to linux will encounter problems in database installation and driver installation (me too). Then, here is the whole process of installing mysql on my Centos6.5 from never installing successfully to successfully executing the sql query. I hope that children's shoes with the same problems can get some help from them.

.

I won't go into details about the installation of mysql database here, but you can read it in another article of mine.

Http://blog.csdn.net/qixin1889/article/details/51298534

Here only talk about how to use the mysql driver to implement a simple query example after the successful installation of the mysql database, and share my experience.

1) after the mysql database is successfully installed, execute the following command with the root user (the installation of mysql must also be root)

# mysqladmin-uroot password 'root'

Change the password of the default Super Admin root during mysql database installation (note: this is the user of mysql, not the root of your linux system) to "root" or something else.

2), log in to the database

# mysql-u root-p

3) create a new database mydb

Create database mydb

4) create table test in mydb

Use mydb; create table test (id varchar (20), name varchar (20)); insert into test vlaues ('01th); insert into test vlaues (' 02th)

The above is to create the table test, the fields are id and name, and insert two pieces of data.

5) enter the home directory to write the test class

# vi Test.javaimport java.sql.*;public class Test {public static void main (String [] args) {try {Class.forName ("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection ("jdbc:mysql://127.0.0.1:3306/mydb?us er=root&password=root"); Statement st = con.createStatement (); ResultSet rs = st.executeQuery ("select * from test") While (rs.next ()) {System.out.println ("id=" + rs.getString (1) + ", name=" + rs.getString (2));} catch (Exception e) {e.printStackTrace ();}

Save and exit.

6), compile and run

# javac Test.java

If you report it correctly, you can run it.

# java Test

The following error message will be prompted

Obviously, the driver for mysql cannot be found, so the next step is to solve the driver problem.

First of all, download the mysql driver, there are many on the Internet, I will not list the address. I use this version of mysql-connector-java-5.1.38-bin.jar.

Then, create a new folder mysql-con under / opt (name whatever, as long as there is no conflict), and put the downloaded driver in mysql-con.

Finally, and crucially, go to the / etc directory and add the path to the mysql driver in the CLASSPATH of the profile file, mine as follows

Then use the source profile command to make the modified file effective, which saves the trouble of logging back in. Then if you execute java Test again, it will show

Id=01,name=test01id=01,name=test02

This means that the driver has been loaded successfully.

Tags: Data database driver success problem file command user directory children's shoes query login run no obvious next lieutenant general example information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Microsoft Huawei vpn