寫(xiě)在前面 這篇文章是對(duì)是Mysql的簡(jiǎn)單語(yǔ)法和與Myeclipse連接健爬,是接著之前我所寫(xiě)的Html部分寫(xiě)的 后面將前面的小知識(shí)點(diǎn)都用到 寫(xiě)一個(gè)簡(jiǎn)單的連接數(shù)據(jù)庫(kù)的web項(xiàng)目
Mysql數(shù)據(jù)庫(kù)語(yǔ)法
1、創(chuàng)建數(shù)據(jù)庫(kù)
create database 0526db遂蛀;//創(chuàng)建數(shù)據(jù)名為0526db的數(shù)據(jù)
2、進(jìn)入0526db數(shù)據(jù)
use 0526db干厚;
進(jìn)入當(dāng)前的數(shù)據(jù)庫(kù)才可以在當(dāng)前數(shù)據(jù)里創(chuàng)建表
3李滴、創(chuàng)建表格
create table t_person //創(chuàng)建表格
(
pid varchar(10) not null primary key, //名稱(chēng) 數(shù)據(jù)類(lèi)型 是否可以為空 設(shè)置為主鍵
pname varchar(20) not null ,
age int ,
address varchar(50) default '未知地址' //default 表示設(shè)置默認(rèn)值
);
4、添加數(shù)據(jù)
alter table t-person //alter table 表名
add column gender varchar(10); //add column 名稱(chēng) 數(shù)據(jù)類(lèi)型蛮瞄;
5所坯、刪除表與顯示表
desc t_person; //顯示表drop 表名
drop table t_person;//刪除表drop table 表名
6、使用Mysql自帶的函數(shù)查詢(xún)
select max(age) from t_person;//查找年齡的最大值
select min(age) from t_person;//查找年齡最小的值
select avg(age) from t_person; //年齡平均值
select count(*) from t_person;//數(shù)據(jù)的總數(shù)
7挂捅、添加數(shù)據(jù)
insert into t_person
select '2','Dasiy','21','China','female'
union //插入多條數(shù)據(jù)的連接符
select '3','Word','24','USA','male';
8芹助、查找語(yǔ)句
select *from t_person; // 顯示表內(nèi)所有內(nèi)容
select *from t_person where age>20;//查找表內(nèi)年齡大于19的
select *from t_person where age>19 and address='LuAn';//多個(gè)查詢(xún)條件使用and連接
9、內(nèi)連接和外連接
select *from t_person t1
inner join t_score t2
on t1.pid= t2.pid;
//inner join 內(nèi)連接 on后面跟條件 當(dāng)t1表的sid值與t2表的stuid相同時(shí)t1
注:這里我只是寫(xiě)了一些簡(jiǎn)單的語(yǔ)法知識(shí),更深的還需我們?nèi)W(xué)習(xí)
Mysql數(shù)據(jù)庫(kù)與Myeclipse連接
public class BaseDAO<> {
private static String URL = "jdbc:mysql://localhost:3306/persondb";
//jdbc:mysql://MyDbComputerNameOrIP:3306/MyDatabaseName
//jdbc:mysql://主機(jī)名或者IP:3306/數(shù)據(jù)庫(kù)名
private static String USER = "root"; //Mysql用戶(hù)名
private static String PWD = "0000";//密碼
private Connection conn;
private Connection getConn() {
//連接數(shù)據(jù)庫(kù)
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) {
//關(guān)閉數(shù)據(jù)庫(kù)
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}