MySql数据库语句和与Myeclipse连接
2016-05-30 本文已影响314人
小夫特
写在前面 这篇文章是对是Mysql的简单语法和与Myeclipse连接,是接着之前我所写的Html部分写的 后面将前面的小知识点都用到 写一个简单的连接数据库的web项目
Mysql数据库语法
1、创建数据库
create database 0526db;//创建数据名为0526db的数据
2、进入0526db数据
use 0526db;
进入当前的数据库才可以在当前数据里创建表
3、创建表格
create table t_person //创建表格
(
pid varchar(10) not null primary key, //名称 数据类型 是否可以为空 设置为主键
pname varchar(20) not null ,
age int ,
address varchar(50) default '未知地址' //default 表示设置默认值
);
4、添加数据
alter table t-person //alter table 表名
add column gender varchar(10); //add column 名称 数据类型;
5、删除表与显示表
desc t_person; //显示表drop 表名
drop table t_person;//删除表drop table 表名
![](https://img.haomeiwen.com/i1958857/656799588acca4f1.png)
6、使用Mysql自带的函数查询
select max(age) from t_person;//查找年龄的最大值
select min(age) from t_person;//查找年龄最小的值
select avg(age) from t_person; //年龄平均值
select count(*) from t_person;//数据的总数
7、添加数据
insert into t_person
select '2','Dasiy','21','China','female'
union //插入多条数据的连接符
select '3','Word','24','USA','male';
8、查找语句
select *from t_person; // 显示表内所有内容
select *from t_person where age>20;//查找表内年龄大于19的
select *from t_person where age>19 and address='LuAn';//多个查询条件使用and连接
![](http://upload-images.jianshu.io/upload_images/1958857-1cbb80859052b8d5.png)
9、内连接和外连接
select *from t_person t1
inner join t_score t2
on t1.pid= t2.pid;
//inner join 内连接 on后面跟条件 当t1表的sid值与t2表的stuid相同时t1
![](http://upload-images.jianshu.io/upload_images/1958857-160576a011e5d9dd.png)
注:这里我只是写了一些简单的语法知识,更深的还需我们去学习
Mysql数据库与Myeclipse连接
public class BaseDAO<> {
private static String URL = "jdbc:mysql://localhost:3306/persondb";
//jdbc:mysql://MyDbComputerNameOrIP:3306/MyDatabaseName
//jdbc:mysql://主机名或者IP:3306/数据库名
private static String USER = "root"; //Mysql用户名
private static String PWD = "0000";//密码
private Connection conn;
private Connection getConn() {
//连接数据库
try {
Class.forName("org.gjt.mm.mysql.Driver");
return DriverManager.getConnection(URL, USER, PWD);
//返回是否连接成功
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private void close(Connection conn) {
//关闭数据库
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}