MySql數(shù)據(jù)庫(kù)語(yǔ)句和與Myeclipse連接

寫在前面 這篇文章是對(duì)是Mysql的簡(jiǎn)單語(yǔ)法和與Myeclipse連接,是接著之前我所寫的Html部分寫的 后面將前面的小知識(shí)點(diǎn)都用到 寫一個(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, //名稱 數(shù)據(jù)類型 是否可以為空 設(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 名稱 數(shù)據(jù)類型;

5、刪除表與顯示表

desc t_person; //顯示表drop 表名
drop table t_person;//刪除表drop table 表名
執(zhí)行上述操作后的t_person表

6、使用Mysql自帶的函數(shù)查詢

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è)查詢條件使用and連接
查找年齡大于20 的結(jié)果

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

內(nèi)連接結(jié)果

注:這里我只是寫了一些簡(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用戶名
    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();
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是數(shù)據(jù)庫(kù)? 數(shù)據(jù)庫(kù)是存儲(chǔ)數(shù)據(jù)的集合的單獨(dú)的應(yīng)用程序。每個(gè)數(shù)據(jù)庫(kù)具有一個(gè)或多個(gè)不同的API,用于創(chuàng)建,訪問(wèn),管理...
    chen_000閱讀 4,124評(píng)論 0 19
  • MySQL數(shù)據(jù)庫(kù)對(duì)象與應(yīng)用 2.1-MySQL數(shù)據(jù)類型 庫(kù)建立好之后基本不動(dòng),和我們接觸最頻繁的是表. 建表就是聲...
    極客圈閱讀 2,250評(píng)論 0 8
  • 理解了之前一位同事跟我說(shuō)他看到手機(jī)呼吸燈亮了之后心跳加速的心情 前兩周考試三門,還被各種事務(wù)纏身,總是在注意力剛回...
    田二二閱讀 636評(píng)論 0 0
  • 莫名的憂傷
    彧馥文芳閱讀 227評(píng)論 0 0

友情鏈接更多精彩內(nèi)容