java——資料庫操作

來源:互聯網
上載者:User

標籤:

1.JDBC的驅動程式有幾種

1>JDBC-ODBC

依靠ODBC磁碟機和資料庫通訊,將ODBC二進位代碼載入到使用該驅動程式的客戶機上

2>本地API

把客戶機的API上的JDBC調用轉換為Oracle,DB2或其他DBMS的調用

3>JDBC網路驅動程式

將JDBC轉換為與DBMS無關的網路通訊協定,又被某個伺服器轉換為一種DBMS協議。

4>本地協議驅動

將JDBC直接轉換為DBMS所使用的網路通訊協定,允許客戶機上直接調用DBMS伺服器

2.預存程序

調用無參數的預存程序

create procedure insertProcedure as begin insert into tb values(‘‘);end;

DriverManager.getConnection().prepareCall("{call insertProcedure()}");//調用預存程序

調用有參數的預存程序

create procedure validateSelect

@userName varchar(20)

as select * from tb where [email protected]

DriverManager.getConnection().prepareCall("{call validateSelect(‘mr‘)}");調用預存程序

3.

資料:資料庫儲存的基本對象,包括數字文字圖形映像等

資料庫:有組織有共用的資料集合

資料庫管理系統:資料庫系統核心軟體,在作業系統支援下操作。資料定義,資料操作,運行管理,建立維護等

資料庫系統:引入資料庫後的系統,包括資料庫,資料庫管理系統

4.Statement和PreparedStatement區別

Statement用於執行靜態SQL,必須事先準備好

PreparedStatement表示先行編譯,解析編譯,放命令緩衝區。運行同一個PreparedStatement對象時,只解析不編譯,減少編譯次數

5.交易處理

setAutoCommit()事務自動認可

rollback()交易回復

commit()事務手動提交

6.獲得表結構

connection----createStatement()----executeQuery()----

getMetaData()----getColumnCount()----

                          getColumnName(i)

                         getColumnTypeName(i)

ResultSetMetadata   擷取關於ResultSet對象中列出的類型和屬性的資訊

ResultSet  儲存查詢資料的結果集

7.資料庫儲存圖片

SQL Server:Image

MySQL:Blob img=rs.getBlob("picture")---new Image(img.getBytes(1,(int)img.length()))

in=FileInputStream(new File(filepath))

setBinaryStream(2,in,(int)new File(filepath).length())

8.SQL最佳化

1>主鍵長度不要太長

2>長度固定的字元欄位應使用char或者nchar

3>長度不固定----varchar或者nvarchar

4>對可有可無的欄位應該給出一個預設值

5>對查詢頻率高的欄位建立索引

6>避免使用like,in,exists,not,<>,!>,!<等

7>避免where中使用函數

8>使用join和子查詢時,優先考慮join

9>少用distinct,order by,聯集查詢union all少用union

10>盡量使用預存程序

9.MYSQL中文亂碼

1>設定資料表及其欄位的編碼為utf-8

2>指定資料連線字串的參數,指定向MYSQL資料庫伺服器發送SQL語句的編碼方式一樣為utf-8

10.statement----connection.createStatement()

statement.executeUpdate()        statement.executeQuery()

 insert into table(name,price) values (‘‘,‘‘)                            while(rs.next())rs.getInt(1),rs.getFloat(3),rs.getString().trim()

11.純文字存入和讀取資料庫

存入:

資料表中儲存大文字檔的列設定為TEXT類型,fileChooser.getSelectedFile().getAbsolutePath()

                 setFileFilter(filter)

insert into tb_text (text) values (?)

connecttion.prepareStatement(sql).setAsciiStream(1,in,(int)file.length()).executeUpdate()

通過流將資料傳到資料庫,流既可以是java流對象,也可以是實現標準介面的子類

讀取:

select text from tb_text 

conn.prepareStatement(sql).executeQuery().next().getAsciiStream(1)

                                           ResultSet             InputStream in--BufferReader(new InputStreamReader(in))----readLine()

12.擷取資料庫中所以表

JDBC中的DatabaseMetaData對象,getTables(要檢索的類別名稱,模式名稱,要檢索的表名稱,表類型的類別)

connection.getMetaData()----getTables(null,null,"%",{"TABLE"})----rs.getString(3)使用者表名稱

DatabaseMetaData                ResultSet             

Oracle :select table_name from user_tables

13.如何?MYSQL的備份與恢複

備份:mysqldump-----Runtime類輸入cmd命令,執行mysqldump

String back="mysqldump -u"+username+" -p"+password+""+database;

Process p=Runtime.getRuntime().exec("cmd.exe /c"+back);

new BufferedReader(new InputStreamReader(p.getInputStream(),"utf-8"))--readLine

new OutputStreamWriter(new FileOutputStream(path),"utf-8")---writer(sb.toString())

 

恢複:從備份檔案中讀取SQL語句,並使用runtime類向當前環境輸入cmd命令,並執行MySQL命令

String back="mysqldump -u"+username+" -p"+password+""+database;

Process p=Runtime.getRuntime().exec("cmd.exe /c"+back);

p.getOutputSream()

new BufferedReader(new InputStreamReader(new FIleInputSTream(pt),"utf-8"))----readLine

new BufferedWriter(new OutputStreamWriter(p.getOutputStream(),"utf-8"))

14.多表串連查詢

A,B公用部分C

1>等值串連查詢(內串連)table1 inner join table2 on 

只返回所有匹配的行,包括重複列,都存在的才查詢出來----C

2>外串連(left join ,right join)table1 right join table2 on 

對內串連的擴充,查詢完整性,不遺失資料

left join:A+C

right join:B+C

3>完全串連--full join

A+B,不合格資料,左右表相應列填上null值

(union all保留重複列)select * from tb_dept union select * from tb_dept1

15.日期儲存

java的Date——>資料庫的datetime

conn.prepareStatement()---setTimestamp(1,new Timestamp(format.parse(String).getTime()))----executeupdate()

conn.createStatement().executeQuery("select * from tb_date")----getMetaData().getColumnCount()

new Timestamp(rs.getDate(1).getTime()).toString().subString(0,10)

Oracle:todate

SQL Server:CONVERT

java——資料庫操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.