国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

centos7安裝mysql并jdbc測試實例詳解

2019-11-02 16:59:49
字體:
來源:轉載
供稿:網友

centos7安裝mysql并jdbc測試實例詳解

前言:

之前用rpm安裝方式安裝不成功,換成yum安裝之后安裝ok了,在網上搜索到很多的rmp安裝和tar包安裝的方式,但是是centos7.x與centos6.x做了很大的改變,可能別人的6.x不適合7.x的安裝,尤其是對于像博主一樣的新人來說,照搬教程可能導致安裝不成功,如果你rmp安裝失敗,那么嘗試跟著本教程來吧。

先卸載已經存在的MySQL。

 [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

更新mysql的yum源

[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安裝

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

不出意外,mysql就已經安裝成功,因為yum安裝是傻瓜式安裝嘛,現在啟動mysql服務。

[root@shizongger bin]# service mysqld start

查看mysql是否啟動,可以監聽它的端口號,mysql監聽的段口號是3306,現在我們來監聽3306端口是否已經啟動:

[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 -

mysql服務已經起來了,可以登陸數據庫了,第一次登陸數據密碼為空,然后再去給數據庫配置root的密碼。

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

用新的密碼登陸

  [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 |  +--------------------+

到這里已經成功的登陸本地數據庫了,停止mysql服務的命令是:

[root@shizongger bin]# service mysqld stop

好了,本地的mysql服務已經搭建起來了,作為一個Java程序員,那么下面請繼續

測試jdbc

首先需要準備好jar包,mysql只需要一個jar包:mysql-connector-java-3.0.14-production-bin.jar,如果你是用vi/vim作為編輯工具,那么你的jar包需要放在jdk的lib文件夾下面,或者放在一個獨立的地方,然后將其加入classpath里面,我在centos下面用ide eclipse來開發,這里以eclipse為例子說來(感覺自己在Linux用ide,有點low).復制+粘帖,放在項目的lib下面。接著來開放我們的jdbc測試用例。

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();      }    }  }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新民市| 鹤山市| 新建县| 巫溪县| 仁化县| 九寨沟县| 惠来县| 丘北县| 古田县| 彭州市| 蓝田县| 定兴县| 镇江市| 浦城县| 肥东县| 醴陵市| 云龙县| 资溪县| 霍邱县| 崇文区| 两当县| 台北市| 凌云县| 宝清县| 白沙| 灌阳县| 客服| 阳朔县| 云阳县| 息烽县| 泰来县| 通河县| 徐汇区| 玉龙| 贡觉县| 清远市| 乌拉特中旗| 汕头市| 同仁县| 吴江市| 宝坻区|