安装mysql

Mac使用homebrew安装mysql,命令行执行以下命令:brew install mysql

启动mysql服务

安装完成后执行start 命令。

➜  ~ mysql.server start
Starting MySQL
. SUCCESS!

连接数据库

命令行方式

默认用户是root,密码为空。

➜  ~ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12 Homebrew Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

JDBC方式

导入JDBC jar包

通过maven方式引入
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
下载后,add进项目

可以直接在mysql官网下载jar包,复制在项目里面,然后添加进依赖即可。

连接mysql

package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBhelper{
public static void main (String[] args){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
conn.close();
} catch(SQLException e){
e.printStackTrace();
}
}
}

数据库操作

数据库操作指令

  1. 创建:create database test_db;
  2. 查询数据库:show databases;
  3. 删除数据库:drop test_db;

增删改查操作指令

命令行方式

插入数据
insert into 表名 (列名....) values (数据....); 删除数据,不添加where条件会清楚整个表的数据
delete from 表名 where 条件; 更新数据,修改id为9的内容name为空
update 表名 set name='' where id=9; 查询数据
select * from table; select 列名 from table; select * from table where id = 9; select * from table order by id; 连表查询
select t.id, tt.name from table1 as t inner join table2 as tt on t.id = tt.u_id;

JDBC操作增删改查

更新数据

插入,更新和删除都是对数据的更新操作,用相同的方法,只是传入的sql语句不同。

package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; public class TransactionTest {
public static String url = "jdbc:mysql://localhost:3306/test";
public static String user = "root";
public static String password = "";
public static String driverName = "com.mysql.jdbc.Driver"; public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} public static void update(String sql) {
Connection conn = getConnection();
try {
Statement st = conn.createStatement();
int count = st.executeUpdate(sql);
System.out.println("更新了 " + count + " 条数据");
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} }
}

查询数据

package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/test";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = ""; Connection conn;
PreparedStatement pst;
ResultSet ret; private void ConnectionDB() {
try {
Class.forName(name);
conn = DriverManager.getConnection(url,user,password);
pst = conn.prepareStatement("select * from myclass;");
ret = pst.executeQuery();
int col = ret.getMetaData().getColumnCount();
while (ret.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(ret.getString(i) + "\t");
if ((i == 2) && (ret.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
ret.close();
pst.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
DBHelper dbHelper = new DBHelper();
dbHelper.ConnectionDB();
}
}

JDBC处理事务

事务是为了保证数据的一致性,要么事务中的数据操作语句都成功,失败的话立即回滚。

package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; public class TransactionTest {
public static String url = "jdbc:mysql://localhost:3306/test";
public static String user = "root";
public static String password = "";
public static String driverName = "com.mysql.jdbc.Driver"; public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} public static void update(String sql) {
Connection conn = getConnection();
try {
Statement st = conn.createStatement();
int count = st.executeUpdate(sql);
System.out.println("更新了 " + count + " 条数据");
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} } public static void rollbackTest() {
Connection conn = getConnection();
try {
conn.setAutoCommit(false);
Statement st = conn.createStatement();
st.executeUpdate("insert into Websites values (8,'tutu','url',99,'CN')");
st.executeUpdate("insert into access_log values ()");
conn.commit();
st.close();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
rollbackTest();
} }

mysql基础操作记录的更多相关文章

  1. MYSQL基础操作

    MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...

  2. MYSQL 基础操作

    1.MySQL基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t1; --创建一个和t1一样的表,用like(表结构也一样 ...

  3. 【MySQL】MySQL基础操作语句

    mysql基础操作语句,包括数据库的增.删.切换,以及表的增.删.改.查.复制. 创建数据库 mysql> create database tem; 使用数据库 mysql> use te ...

  4. MySQL基础操作&&常用的SQL技巧&&SQL语句优化

    基础操作     一:MySQL基础操作         1:MySQL表复制             复制表结构 + 复制表数据             create table t3 like t ...

  5. mysql数据库优化课程---13、mysql基础操作

    mysql数据库优化课程---13.mysql基础操作 一.总结 一句话总结:mysql复制表,索引,视图 1.mysql如何复制表? like select * 1.复制表结构 create tab ...

  6. MySQL基础操作(二)

    MySQL基础操作 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用.注意:使用视图时 ...

  7. 前端笔记之服务器&Ajax(中)MySQL基础操作&PHP操作数据库&Ajax

    一.数据库基础 1.1什么是数据库? 什么是数据库? 答:就是一个很大的一个文件,只不过这个文件可以通过一些‘命令’操作数据: 增.删.改.查数据: 数据库等于持久数据和数据操作的一个统称. 数据库是 ...

  8. 02 . Mysql基础操作及增删改查

    SQL简介 SQL(Structured Query Language 即结构化查询语言) SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语句四大 ...

  9. PHP mysql基础操作

    mysql连接操作 //建立连接$con = mysql_connect('localhost', 'root', '123456');//判断是否连接成功if($con){ die('连接失败!'. ...

随机推荐

  1. PTA 1005 Spell It Right (20)(20 分)水题

    1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...

  2. zufeoj NO.1(结构体简单题)

    NO.1 时间限制: 1 Sec  内存限制: 128 MB提交: 457  解决: 172[提交][状态][讨论版] 题目描述 所谓NO.1,就是所有成绩都排在第一的同学,我们假设每个人只有理科,文 ...

  3. IJ

    ylbtech-IJ: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   10.返回 ...

  4. Mac运维安装软件

    Maccrt软件 sudo spctl --master-disable 打开软件,复制到app,按照sn.txt输入即可 sudo spctl --master-enable crt快捷键crtl ...

  5. css3的transition属性的使用

    transition是将某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画效果.这个属性一般搭配:hover来使 下面看一个例子:鼠标放在div上,0.2s后将div元素的背景色用一秒 ...

  6. News Master-DC and Marvel they are super heroes mother

    News Master Good evening everyone,I’m Jason,I’m glad to be news master to share something, Tonight I ...

  7. 【转】C#调用java类、jar包方法

    原文地址:http://blog.csdn.net/black0707/article/details/5769366 一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录 ...

  8. Tkinter Bitmaps

       Tkinter Bitmaps: 你会使用这个属性显示一个位图.有以下类型的可用位图. 你会使用这个属性显示一个位图.有以下类型的可用位图.: "error" "g ...

  9. DataMatrix二维条码源码分析检测识别图像位置

    发布时间:2014-10-31 DataMatrix的代码结构和QR码基本相同: 其中Detector的功能还是从原始图像中找出符号码的部分,并且进行透视转换纠正扭曲. 其解码流程与QR码差不多,关键 ...

  10. 上传项目到git

    …or create a new repository on the command line   echo "# test" >> README.md git ini ...