码迷,mamicode.com
首页 > 数据库 > 详细

java18(MySQL)

时间:2017-09-28 18:06:49      阅读:304      评论:0      收藏:1      [点我收藏+]

标签:har   查看   mit   exists   des   upd   init   管理器   manager   

sc

service console,服务控制台程序,可以启动,停止,删除等服务

sc start MySQL

sc stop MySQL

sc delete MySQL        //删除,从注册表删除服务记录

MySQL常用指令

mysql -h hostname -u root -p password      //连接远程数据库

mysql -u root -p password            //连接localhost

mysql  --help                  //查看帮助

mysql>select current_date;            //

mysql>select now();               //函数

mysql>-- 这是注释

mysql>show databases;             //显示所有数据库

mysql>drop database mydatabase;        //删除某个数据库

mysql>create database mydatabase;       //创建数据库

mysql>use mydatabase;             //指定使用数据库

mysql>show tables;               //显示表

mysql>create table tablename testtable(id varchar(20), psw varchar(20));//创建表

mysql>describe testtable;   或 desc testtable;    //查看表结构

mysql>drop table testtable;            //删除表

 

 

mysql>select * from testtable;           //全字段,全表扫描

mysql>select * from testtable where id > 3;

mysql>select * from testtable where id >3 and id <5;

mysql> select * from testtable where id = ‘1‘;

mysql> select * from testtable where psw like ‘1%‘;    //以1开头的,模糊查询

mysql> select * from testtable where psw not like ‘1%‘;    //不是以1开头的

mysql> select * from testtable where psw like ‘1_‘;    //以1开头的接着是任意一个字符

mysql> select * from testtable where psw like ‘1\_‘;    //以1开头的接着是任意一个_(转义字符查询特殊字面量)

mysql> select * from testtable order by id desc;    //j降序查询

mysql> select * from testtable order by id asc;

mysql> select * from testtable order by age asc, id desc;  //组合排序

mysql>select id, psw from testtable;        //投影查询

mysql>select * from testtable where psw is null;  //查询psw为null

mysql>select * from testtable where psw is not null;  //查询psw非null

mysql>select count(*) from testtable where psw = "123456";//聚集函数查询

mysql>select max(age) from testtable;

mysql>select min(age) from testtable;

mysql>select avg(age) from testtable;

mysql>select sum(age) from testtable where id < 5;

mysql>select * from testtable limit 1, 3;

mysql>select * from testtable limit 0, 3; =====select * from testtable limit 0. 3;//分页查询

mysql>insert into testtable(id, psw) values(‘1‘, ‘pass‘);//插入

mysql>insert into testtable(id) values(‘1‘);//插入部分字段

mysql>insert into testtable values(‘1‘, ‘pass‘);//插入全部字段

mysql>update testtable set psw = ‘123456‘ where id = ‘1‘;//修改

 mysql>update testtable set age = 20 where id in (5, 6);        //in

CRUD

[create]

 insert into table_name(field_name,...) values(value,...);

[retrieve]

 selete id,... from table_name where id = xxx,...;

[update]

 update table_name set id = xxx, ... where id = xxx, ...;

[delete]

delete from table_name where ...;

 source d:/findtest.sql    //执行脚本文件

 

MySQL约束

1.primary key (非空,不能重复)

create tabe testtable(id int primary key, ...);

2.自增

create tabe testtable(id int primary key auto_increment, ...);

3.带条件创建

create database if not exists testbase;

create table if not exists test;

 

drop database if exists;

 

JDBC

    java database connection, socket

    //JDBCDemo.java

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.Statement;
 4 
 5 public class JDBCDemo {
 6 
 7     public static void main(String[] args) {
 8         // 注册驱动程序
 9         String url = "jdbc:mysql://localhost:3306/mytestbase";
10         String user = "root";
11         String password = "root";
12         // 驱动器管理器
13         try {
14             Class.forName("com.mysql.jdbc.Driver");
15             Connection connec = DriverManager.getConnection(url, user, password);
16             // 创建语句对象
17             Statement statement = connec.createStatement();
18             String sql = "insert into testtable values(‘6‘, ‘opii‘)";
19             statement.execute(sql);
20             System.out.println("over");
21             // 释放资源
22             statement.close();
23             connec.close();
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27     }
28 
29 }

  //TestCRUD.java

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.Statement;
  4 
  5 import org.junit.Before;
  6 import org.junit.Test;
  7 
  8 import com.mysql.jdbc.ResultSet;
  9 
 10 /**
 11  * 测试增删查改
 12  * 
 13  */
 14 public class TestCRUD {
 15     
 16     private Connection connec;
 17 
 18     /**
 19      * 先执行
 20      */
 21     @Before
 22     public void initConn() {
 23                 String url = "jdbc:mysql://localhost:3306/mytestbase";
 24                 String user = "root";
 25                 String password = "root";
 26                 // 驱动器管理器
 27                 try {
 28                     // 注册驱动程序
 29                     Class.forName("com.mysql.jdbc.Driver");
 30                     connec = DriverManager.getConnection(url, user, password);
 31                 } catch (Exception e) {
 32                     e.printStackTrace();
 33                 }
 34     }
 35     /**
 36      * insert
 37      */
 38     @Test
 39     public void insert() {
 40         try {
 41             // 创建语句对象
 42             Statement statement = connec.createStatement();
 43             String sql = "insert into testtable values(‘7‘, ‘opiill‘)";
 44             statement.execute(sql);
 45             System.out.println("over");
 46             // 释放资源
 47             statement.close();
 48             connec.close();
 49         } catch (Exception e) {
 50             e.printStackTrace();
 51         }
 52     }
 53     
 54     /**
 55      * update
 56      */
 57     @Test
 58     public void update() {
 59         try {
 60             // 创建语句对象
 61             Statement statement = connec.createStatement();
 62             String sql = "update testtable set psw=‘newpass‘ where id = ‘7‘";
 63             statement.execute(sql);
 64             System.out.println("over");
 65             // 释放资源
 66             statement.close();
 67             connec.close();
 68         } catch (Exception e) {
 69             e.printStackTrace();
 70         }
 71     }
 72     
 73     /**
 74      * delete
 75      */
 76     @Test
 77     public void delete() {
 78         try {
 79             // 创建语句对象
 80             Statement statement = connec.createStatement();
 81             String sql = "delete from testtable where id = ‘7‘";
 82             statement.execute(sql);
 83             System.out.println("over");
 84             // 释放资源
 85             statement.close();
 86             connec.close();
 87         } catch (Exception e) {
 88             e.printStackTrace();
 89         }
 90     }
 91     
 92     /**
 93      * query
 94      */
 95     @Test
 96     public void query() {
 97         try {
 98             // 创建语句对象
 99             Statement statement = connec.createStatement();
100             String sql = "select * from testtable";
101             ResultSet rs = (ResultSet) statement.executeQuery(sql);
102             while (rs.next()) {
103                 String id = rs.getString("id");
104                 String psw = rs.getString("psw");
105                 System.out.println(id + ":" + psw);
106             }
107             System.out.println("over");
108             // 释放资源
109             rs.close();
110             statement.close();
111             connec.close();
112         } catch (Exception e) {
113             e.printStackTrace();
114         }
115     }
116 }

 

java18(MySQL)

标签:har   查看   mit   exists   des   upd   init   管理器   manager   

原文地址:http://www.cnblogs.com/8386blogs/p/7606207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!