煎熬

重装系统之后,一切还要比方更起首折腾,这一次把有操作记下来,未来或还相会用到。

·写一个简易的Java 程序,连接受数据库并出口查询消息

新建一个 Java
Project,把jdbc驱动包包括上路 Build Path

新建一个Hello.java

情节如下:

 

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 public class Hello {
 8 
 9     public static void main(String[] args) throws ClassNotFoundException, SQLException {
10         Class.forName("com.mysql.jdbc.Driver");    
11         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/feinotes","root","123");
12         Statement stmt = con.createStatement();
13         String query = "select * from user";
14         ResultSet rs = stmt.executeQuery(query);
15         while(rs.next()){
16             System.out.println("id:"+rs.getInt(1)+" name:"+rs.getString(2));
17         }
18     }
19 
20 }

运作结果及预期:

id:1 name:sff2013
id:2 name:sff2014

稍许改变一下,查询note表

1 String query = "select * from note";
2 
3 System.out.println("title:"+rs.getString(2)+" content:"+rs.getString(3));

 

结果如下,在此以前的体现乱码并不曾影响著

 

title:标题三 content:内容三
title:标题四 content:内容四

 

·MySQL

当 Windows10 上安好了
MySQL5.5,然后就是会见现出布局窗口,用于配置 MySQL
的有音讯。弄好下,点击起始菜单,找到 MySQL 5.5
命令行客户端,打开这就是得一贯进去 MySQL 控制台了。当然为足以拿 bin
目录上加至网环境变量 PATH 中,直接当命令行里操作。

开拓 MySQL
命令行工具,直接输入密码(默认root用户),出现 mysql> 就象征连接上的
MySQL 服务器。

亮当前版,当前时看看:

1 mysql> select version(),current_date,now();
2 
3 +-----------+--------------+---------------------+
4 | version() | current_date | now()               |
5 +-----------+--------------+---------------------+
6 | 5.5.20    | 2016-04-21   | 2016-04-21 15:47:55 |
7 +-----------+--------------+---------------------+

 

 

 

搜寻来当下服务器上有的数据库:

 1 mysql> show database;
 2 
 3 +--------------------+
 4 | Database           |
 5 +--------------------+
 6 | information_schema |
 7 | mysql              |
 8 | performance_schema |
 9 | test               |
10 +--------------------+

 创办数据库:

mysql> create databases feinotes;

选拔数据库:

1 mysql> use feinotes;
2 Database changed

 创建表

这会儿数据库是空的,

1 mysql> show tables;
2 Empty set

待创制一个 user 表和一个 note

user表:

int id;(主键)

String name;

String username;

String password;

note表

int id;(主键)

String title;

text content;

Timestamp createTime;

int userId;(外键)

创建 user 表

 

1 mysql> create table user(
2        -> id int unsigned not null auto_increment primary key,
3        -> name varchar(100) not null,
4        -> username varchar(100) not null,
5        -> password varchar(100) not null);

 

 

 

开创表错了,删除表还成立

1 mysql> drop table user;

创办成功翻转表结构

 1 mysql> describe user;
 2 
 3 +----------+------------------+------+-----+---------+----------------+
 4 | Field    | Type             | Null | Key | Default | Extra          |
 5 +----------+------------------+------+-----+---------+----------------+
 6 | id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
 7 | name     | varchar(100)     | NO   |     | NULL    |                |
 8 | username | varchar(100)     | NO   |     | NULL    |                |
 9 | password | varchar(100)     | NO   |     | NULL    |                |
10 +----------+------------------+------+-----+---------+----------------+

创建 note 表

 1 mysql> create table note(
 2     -> id int unsigned not null auto_increment primary key,
 3     -> title varchar(100) not null,
 4     -> content text,
 5     -> createTime timestamp ,
 6     -> userId int unsigned,
 7     -> foreign key(unsigned int userId)references user(id));

长外键有或会面油不过生 ERROR 1005
(HY000)错误,这一般是主键和外键字段类型不相同致的。

创立成功后翻转发明:

 

 1  mysql> describe note;
 2 +------------+------------------+------+-----+-------------------+-----------------------------+
 3 | Field      | Type             | Null | Key | Default           | Extra                       |
 4 +------------+------------------+------+-----+-------------------+-----------------------------+
 5 | id         | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
 6 | title      | varchar(100)     | NO   |     | NULL              |                             |
 7 | content    | text             | YES  |     | NULL              |                             |
 8 | createTime | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
 9 | userId     | int(10) unsigned | YES  | MUL | NULL              |                             |
10 +------------+------------------+------+-----+-------------------+-----------------------------+

 

 导入数据

小先下一个器:MySQL Query
Browser,连接上数据库,直接输入数据,即可。

导入完询问一下:

图片 1

在指令行下粤语会显示乱码(不造为啥),可是似乎不影响其实应用

1 mysql> select * from note;
2 +----+-----------+-----------+---------------------+--------+
3 | id | title     | content   | createTime          | userId |
4 +----+-----------+-----------+---------------------+--------+
5 |  1 | 鏍囬涓€    | 鍐呭涓€    | 2016-04-21 16:43:22 |      1 |
6 |  2 | 鏍囬浜?   | 鍐呭浜?   | 2016-04-21 16:43:32 |      1 |
7 |  3 | 鏍囬涓?   | 鍐呭涓?   | 2016-04-21 16:22:22 |      2 |
8 |  4 | 鏍囬鍥?   | 鍐呭鍥?   | 2016-04-21 16:32:33 |      2 |
9 +----+-----------+-----------+---------------------+--------+
网站地图xml地图