centos7 installs mysql and jdbc tests the details of the example

  • 2020-05-17 07:33:04
  • OfStack

centos7 installs mysql and jdbc tests the example in detail

Preface:

rpm installation way before installation is not successful, installed into yum after ok, search on the Internet to many rmp installation and tar package installed, but is centos7 x and centos6. x made great change, may be someone else's 6. x unsuitable for 7. x installation, especially for new kind like blogger 1 copy tutorial may lead to install is not successful, if you fail to rmp installation, try to follow this tutorial.

Uninstall the MySQL that already exists.


 [root@shizongger bin]# rpm -qa|grep mysql
 [root@shizongger bin]# rpm -qa mysql
 [root@shizongger bin]# rpm -qa|grep -i mysql
 MySQL-client-5.5.54-1.el7.x86_64
 [root@shizongger bin]# rpm -e --nodeps M
 ModemManager ModemManager-glib MySQL-client
 [root@shizongger bin]# rpm -e --nodeps MySQL-client

Update mysql's yum source


[root@shizongger bin]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@shizongger bin]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum installation


[root@shizongger bin]# yum install mysql-community-server

Not surprisingly, mysql has already been installed, because the yum installation is a fool's errand, now start the mysql service.


[root@shizongger bin]# service mysqld start

To check whether mysql is started, you can listen to its port number. The segment number of mysql is 3306. Now let's listen to whether port 3306 has been started:


[shizongger@shizongger src]$ netstat -anp |grep 3306
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::3306 :::* LISTEN -

The mysql service is up and you can log into the database. The password for the first login is empty, and then you can configure the root password for the database.


[root@shizongger bin]# mysql -uroot
mysql> set password for 'root'@'localhost' =password('root');
mysql>exit

Log in with your new password


  [root@shizongger bin]# mysql -uroot -proot
  mysql> show database;
  ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
  mysql> show databases;
  +--------------------+
  | Database |
  +--------------------+
  | information_schema |
  | mysql |
  | performance_schema |
  +--------------------+

At this point, you have successfully logged into the local database. The command to stop the mysql service is:


[root@shizongger bin]# service mysqld stop

Ok, the local mysql service has been set up. As an Java programmer, please continue

Test jdbc

jar package needs only 1 jar package: mysql-connector-java-3.0.14-production-bin. jar. If you are using vi/vim as an editing tool, then your jar package needs to be placed under jdk's lib folder, or in a separate place, and then it should be added to classpath. Here is an example of eclipse (I feel like I'm using ide in Linux, a little low). Copy + paste and put it under lib of the project. Next, open up our jdbc test case.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {

  public static void main(String[] args) {
    Connection conn = null;
    Statement sm = null;
    ResultSet rs = null;

    try {
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://localhost:3306/shopping?zeroDateTimeBehavior=convertToNull";
      conn = DriverManager.getConnection(url, "root", "root");
      sm = conn.createStatement();
      rs = sm.executeQuery("select * from user");
      while(rs.next()) {
        System.out.println("id:" + rs.getInt("id") + " name:" + rs.getString("name"));
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try{
        if(rs != null) {
          rs.close();
          rs = null;
        }
        if(sm != null) {
          sm.close();
          sm = null;
        }
        if(conn != null) {
          conn.close();
          conn = null;
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }

  }

}

After installing mysql, I created shopping's database in my mysql and added user's table to it, with only one int id and one varchar name in the table structure.

At this step, the java open database is almost complete. Installation of linux software will be more troublesome than window, this blog is not in line with the 1-cut machine, when you encounter difficulties or installation failure, please be patient and try your best to finally have a solution.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: