Home Database Mysql Tutorial MySQL安装、配置、使用及JDBC链接

MySQL安装、配置、使用及JDBC链接

Jun 07, 2016 pm 04:18 PM
jdbc mysql use Install Configuration Link

URL: #downloads 选择No thanks, juststart my download. mysql-5.5.36-win32.msi 33.7M mysql-5.5.36-winx64.msi 35.3M Navicat V8.2.12ForMySQL使用: 1. 点击连接,输入自定义的连接名和root账号的密码,建立了连接。 2. 打开连接,后就可以对表和视图等进

   URL: #downloads

  选择No thanks, juststart my download.

  mysql-5.5.36-win32.msi 33.7M

  mysql-5.5.36-winx64.msi 35.3M

  Navicat V8.2.12ForMySQL使用:

  1. 点击连接,输入自定义的连接名和root账号的密码,建立了连接。

  2. 打开连接,后就可以对表和视图等进行操作,非常方便。

  注意:

  1. 如何本地安装,只需要选Development的低连接数就可以了,少占内存,安装的时候顺便配下密码。

  2. 安装好了MySQL之后,最好再安装一个MySQL的插件

  Navicat V8.2.12ForMySQL,可视化,界面友好,是非常好的管理工具。

  使用:

  1. 安装好后,可以在启动栏看到MySQL 5.5 Command Line Client

  2. 点击后输入密码,后即可进入工作模式,或在运行栏输入

  mysql –uroot –p 再输入密码也可。

  mysql>

  注意:

  1) 连接远端主机命令: mysql –h主机地址 –u用户名 –p地址

  假设主机:192.168.0.1 user: root pwd:1234

  则命令: mysql –h198.168.0.1 –uroot –p1234

  2) 此处可用空格,可不用,所以又可以写成

  Mysql –h 192.168.0.1 –u root –p 1234

  3. 退出

  exit

  修改密码:

  1. 增加新密码:Dos进入mysql/bin目录下, 执行下面命令:

  mysqladmin-u root –p1234 //à1234为新增加的密码.

  2. 修改密码:

  mysqladmin -u root–p1234 password abcd //à新密码:abcd

  用户管理:

  格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"

  例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MYSQL,然后键入以下命令:

  grant select,insert,update,delete on *.* to test1@"%"Identified by "abc";

  但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见下例

  例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。

  grant select,insert,update,delete on mydb.* to test2@localhostidentified by "abc";

  如果你不想test2有密码,,可以再打一个命令将密码消掉。

  grant select,insert,update,delete on mydb.* to test2@localhostidentified by "";

  常用命令:

  1. 显示数据库列表

  mysql>show databases;

  +--------------------+

  |Database |

  +--------------------+

  |information_schema |

  |mysql |

  |performance_schema |

  |test |

  +--------------------+

  4 rows in set (0.05 sec)

  2. 使用某个数据库.

  mysql> use mysql; //àmysql自带数据库

  mysql> show tables; //à看多少表

  +---------------------------+

  | Tables_in_mysql |

  +---------------------------+

  | columns_priv |

  | db |

  | plugin |

  +---------------------------+

  3 rows in set (0.25 sec)

  3. 显示表结构

  mysql> desc proc;

  +----------------------+-----------------------------

  | Field | Type

  +----------------------+-----------------------------

  | db | char(64)

  | name | char(64)

  +----------------------+-----------------------------

  2 rows in set (0.21 sec)

  4. 建库

  mysql> create database school; //àschool 数据库名

  drop database if exists school; //à存在则删除;

  5. 建表

  mysql> use school; //à打开库.对表操作必须先选数据库

  mysql>

  create tableteacher

  (

  id int(3)auto_increment not null primary key,

  name char(10) notnull,

  addressvarchar(50) default 'ChengDu',

  year date

  );

  6. 删库|删表

  mysql> drop database school;

  mysql> drop table teacher;

  7. 插入|更新

  insert into teacher values(001,'Wang','BJ','1988-10-10');

  insert into teachervalues(002,'Jiang','SH','1999-10-10');

  update teacher set address=’CD’ wherename=’Wang’

  8. 删除|查询

  mysql> delete * from teacher wherename='Jiang';

  mysql> select * from teacher; //à语句以分号结束

  9. 显示行数|列数

  select count(*) from teacher;

  select sum(*) from teacher;

  注意:如果命令很多,DOS进入mysql/bin下面执行

  mysql -u root -pabcd

  例如: 拿下面的命令放在school.sql里面,同时完成建库,表,插入的操作。

  drop database if exists school;

  create database school;

  use school;

  create table teacher

  (

  id int(3) auto_increment not null primary key,

  name char(10) not null,

  address varchar(50) default 'BeiJing',

  year date

  );

  insert into teachervalues('001','Tom','ChengDu','1976-10-10');

  insert into teachervalues('002','Bob','ShengZH','1975-12-23');

  NetBeansà驱动à访问MySQL

  1. 下载数据库驱动程序:mysql-connector-java-5.0.8,并将此目录下的文件mysql-connector-java-5.0.8-bin.jar放到%JAVA_HOME%lib下.

  注意:如果不在放到这里的话,则需要将文件所在目录加到classpath中。

  2. 打开NetBeans

  所在项目的Libraries 右键Add JAR/Folder 选中刚才的jar文件。

  3. 打开NetBeans: WindowàServicesàDatabase

  在MySQL Server at localhost:3306[root]上右键àProperties上作如下配置:

  注意:

  1. 在Path/URL to admin Tool: 这一项中填上MySQL的管理工具,这里用的是Navicat V8.2.12ForMySQL,所以这里填上该工具可执行文件的具体目录即可,其他选项会默认生成。

  2. Server HostName:

  如果是本地的话就填:localhost 或 127.0.1

  如果是远程的话就填:MySQL数据库服务器所在IP。

  下面NetBeans里面的完整例子来连MySQL,并对其进行操作:

  import java.sql.Connection; //Connection类

  import java.sql.DriverManager; //驱动类

  import java.sql.ResultSet; //查询到的SQL结果集类

  import java.sql.Statement; //SQL语句类

  public class MySQLTest {

  public static void main(String arg[]) {

  try {

  Connection con = null; //定义一个MYSQL链接对象

  Class.forName("com.mysql.jdbc.Driver").newInstance();

  //MySQL驱动

  con =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school","root", "abcd"); //school是数据库名,abcd是root账号的密码。

  Statement stmt; //创建声明

  stmt = con.createStatement(); //新增一条数据

  stmt.executeUpdate("INSERTINTO user (username, password) VALUES ('qgao', '123456')");

  ResultSet res =stmt.executeQuery("select LAST_INSERT_ID()");

  int ret_id;

  if (res.next()) {

  ret_id = res.getInt(1);

  System.out.print(ret_id);

  }

  //删除一条数据

  String sql = "DELETE FROMuser WHERE username='Li Si'";

  long deleteRes =stmt.executeUpdate(sql);

  //如果为0则没有进行删除操作,如果大于0,则记录删除的条数

  System.out.print("DELETE:" + deleteRes);

  //更新一条数据

  String updateSql = "UPDATEuser SET password = '1234' WHERE username='Gao Shou'";

  long updateRes =stmt.executeUpdate(updateSql);

  System.out.print("UPDATE:" + updateRes);

  //查询数据并输出

  String selectSql = "SELECT *FROM user";

  ResultSet selectRes =stmt.executeQuery(selectSql);

  while (selectRes.next()) { //循环输出结果集

  String username =selectRes.getString("username");

  String password =selectRes.getString("password");

  System.out.print("rnrn");

  System.out.print("username:" + username +"password:" + password);

  }

  } catch (Exception e) {

  System.out.print("MYSQLERROR:" + e.getMessage());

  }

  }

  }

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles