標籤:efault 右鍵 init 從表 drop pass name on() 刪除
MySQL部分
1.先建立mysql資料庫資料表
① 運行-mysql –u root –p 輸入密碼(安裝mysql時設定)開啟mysql
② 建立資料庫 create database 資料庫名稱;
show databases ; 查看資料庫
drop database 資料庫名稱 ; 刪除資料庫
③ 選擇資料庫 use 資料庫名稱;
④ 建立資料表
create table 表名稱
(
欄位名稱1 資料類型 約束條件,
欄位名稱2 資料類型 約束條件,
... ...
欄位名稱n 資料類型 約束條件
);
Mysql資料類型
字元:char(字元資料大小個數) 不可變字元 / varchar(資料大小) 可變字元 /text
數字:float/double(7,2) int
日期:date / time / datetime / timestamp
Mysql 約束條件
主鍵約束:primary key -- 標識所有記錄唯一性的標誌,所對應資料 不可為空,且不能重複
非空約束:not null -- 該欄位資訊必須填寫,不能使用null進行填充
唯一約束:unique -- 該欄位資訊不能有重新資訊
外鍵約束:foreign key -- 在從表中引用主表中資料資訊
檢查約束:check -- 檢查當前輸入輸入有效性
預設值: default -- 使用者沒有填加資料時,採用預設值進行填充
Eg:
create table userInfo( id int primary key auto_increment, #編號,整形,主鍵,自動成長 username varchar(20) unique , #唯一性 password varchar(20) not null , #非空 sex char(1) default ‘妖‘, #預設值 age int , birth date null #允許為空白);
⑤ 查看當前資料庫存在表 show tables;
⑥ 查看錶結構資訊 desc 表名稱;
⑦ 刪除資料表 drop table 表名稱;
⑧ 添加資料
inset into 表名稱(欄位名稱1,欄位名稱2,…..) values(數值1,數值2,…..);
insert into userinfo(username,password,sex,age,birth) values(‘張三‘,‘111‘,‘男‘,22,‘2000-1-1‘) ;
⑨ 查看資料
select 欄位名稱1,欄位名稱2,…..from 表名稱;
select id,username,password,sex,age,birth from userinfo ;
⑩ 刪除資料
delete from 表名 where 條件 資料;
delete from userinfo where id=1;
JAVA 部分
2.編寫java檔案
主要步驟:
// 1添加驅動
// 2連結資料庫
// 3操作資料
// 4關閉連結
① 添加驅動:匯入 mysql-connector-java-bin.jar 檔案,右鍵 build path 引用外部jar包
try { // class.forname 獲得class類對象 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫驅動載入失敗..."); }
② 串連資料庫:
try { //DirverManager.getConnection();串連資料庫 conn = DriverManager.getConnection(url,user,pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫連接失敗"); }
③ 操作資料庫
//添加操作
//String sql = "insert into userinfo(username,password,sex,age,birth) values(‘zere‘,‘666‘,‘女‘,24,‘1994-4-17‘)"; //刪除操作 String sql = "delete from userinfo where id=1";try { //statement 方法建立用於執行SQL語句並返回它所產生結果的對象,用connection的方法createstatement建立 sta = conn.createStatement(); //statement 介面中有executeUpdate(String sql);方法發送sql語句並返回執行成功的記錄的條數 int num = sta.executeUpdate(sql); if(num >0){ System.out.println("資料庫操作成功"); }else{ System.out.println("資料庫操作失敗1"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫操作失敗2"); }
④ 關閉資料庫操作對象
try { sta.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
⑤ 運行資料庫連接方法
public static void main(String[] args){ MysqlLink link = new MysqlLink(); link.init(); }
整合代碼:
package com.cz.link; import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement; public class MysqlLink { /*連結步驟 1.添加驅動 2.串連資料庫 3.操作資料 4.關閉連結 */ String url = "jdbc:mysql://localhost:3306/javass"; String user = "root"; String pwd = "123456"; //聲明連結化物件 Connection conn = null; String sql = null; public Statement sta = null; public void init(){ //1.載入驅動 try { // class.forname 獲得class類對象 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫驅動載入失敗..."); } //2.串連資料庫 try { //DirverManager.getConnection();串連資料庫 conn = DriverManager.getConnection(url,user,pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫連接失敗"); } //3.資料操作 //添加操作 //String sql = "insert into userinfo(username,password,sex,age,birth) values(‘zere‘,‘666‘,‘女‘,24,‘1994-4-17‘)"; //刪除操作 String sql = "delete from userinfo where id=1";try { //statement 方法建立用於執行SQL語句並返回它所產生結果的對象,用connection的方法createstatement建立 sta = conn.createStatement(); //statement 介面中有executeUpdate(String sql);方法發送sql並返回執行成功的記錄的條數 int num = sta.executeUpdate(sql); if(num >0){ System.out.println("資料庫操作成功"); }else{ System.out.println("資料庫操作失敗1"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("資料庫操作失敗2"); } //4.關閉資料庫操作對象 try { sta.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args){ MysqlLink link = new MysqlLink(); link.init(); }}
Java連結Mysql傳輸資料