一、JDBC概念
java資料庫的連接配接,(Java Database Connectivity 簡稱JDBC)是Java語言中用來規範用戶端程式如何來通路資料庫的應用程式接口。
連接配接MySQL中的資料庫:jdbc:mysql://localhost:3306/資料庫名稱?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&useSSL=false";
二、加載資料庫驅動
//1,加載驅動
Class.forName("com.mysql.cj.jdbc.Driver");
如果加載的是SQL server:Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
三、建立連接配接
1)資料庫URL:URL用于辨別資料庫的位置,程式員通過URL位址告訴JDBC程式連接配接哪個資料庫。
URL的寫法為:"jdbc:mysql://localhost:3306/資料庫名稱?參數名=參數值..."
常用資料庫URL位址的寫法:
- Oracle:jdbc:oracle:thin:@localhost:2521:資料庫名稱
- SqlServer:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=資料庫名稱
- MySQL:jdbc:mysql://localhost:3306/資料庫名稱
2)Connection:
jdbc程式中的Connection,它用于代表資料庫的連結,Collection是資料庫程式設計中的一個最重要的一個對象,用戶端與資料庫所有互動都是通過Connection對象完成的.
建立方法:Connection conn = DriverManager.getConnection( URL,User,PassWord );
常用方法:
● createStatement();建立向資料庫發送sql的statement對象
● prepareStatement(sql);建立向資料庫發送預編譯sql的PrepareStatement對象
● prepareCall(sql);建立執行存儲過程的callableStatement對象
● setAutoCommit(boolean autoCommit);設定事務是否自動送出
● commit();在連結上送出事務
● rollback();在此連結上復原事務
String url = "jdbc.mysql://localhost:3306/資料庫名稱";
String username = "使用者名";
String password = "使用者密碼";
Connection conn = null;
//2,擷取與資料庫的連結
conn = DriverManager.getConnection(url,username,password);
四、執行SQL語句
1)Statement:jdbc程式中的Statement對象用于向資料庫發送SQL語句。
建立方法:Statement st = conn.createdStatement();
● executeQuery(String sql);用于向資料庫發送查詢語句
● executeUpdate(String sql);用于向資料庫發送insert、update或delete語句
● execute(String sql);用于向資料庫發送任意語句
● addBatch(String sql);把多條SQL語句發送到一個批進行中
● executeBatch();向資料庫發送一批SQL語句執行。
Statement st = null;
//3,擷取用于向資料庫發送SQL語句的Statement
st = conn.createdStatement();
//4,向資料庫發送SQL
String sql = "select id,name,password,email,birthday from users
st.executeQuery();
2)PreparedStatement:該類是Statement的子類對象
建立方法:PreparedStatement st = conn.preparedStatement();
preparedStatement st = null;
String sql = "select * from user where name=? and password = ?";
//3,擷取用于向資料庫發送SQL語句的preparedStatement
st = conn.preparedStatement(sql);//傳入SQL語句,進行預編譯
st.setString(1,username);
st.setString(2,password);
//4,向資料庫發SQL
st.executeQuery();//這裡不用傳入SQL語句了
3)Statement與PreparedStatement的差別
相對于Statement對象而言PreparedStatement可以避免SQL注入的問題。
如:String sql = "select * from admin where loginname=' "+loginName+" ' and loginpwd=' "+loginPwd+" ' ";
在應用中:
- 》請輸入賬号:
333
- 》請輸入密碼:
wer'or'1'='1
實際上發送:select * from admin where loginname=' 333 ' and loginpwd=' wer'or'1'='1 ' ,登入成功!
Statement會使資料庫頻繁編譯SQL,可能會造成資料庫緩沖區溢出。PreparedStatement可對SQL進行預編譯,進而提高資料庫的執行效率。
并且PreparedStatement對于SQL中的參數,允許隻用占位符的形式進行替換,簡化了SQL語句的編寫。
五、擷取結果
jdbc程式中的ResultSet用于代表Sql語句的執行效果。ResultSet封裝執行結果時,采用的類似于表格的方式,ResultSet對象維護了一個指向表格資料行的遊标、初始的時候,遊标在第一行之前調用ResultSet.next()方法,可以使遊标指向具體的資料行,進行調用方法擷取該行的資料。
1)擷取行:ResultSet提供了對結果集進行滾動的方法
● next();移動到下一行
● Previous();移動到前一行
● absolute(int row);移動到指定行
● beforeFirst();移動到ResultSet的最前面
● afterLast();移動到ResultSet的最後面
2)擷取值:ResultSet既然用于封裝執行結果的,是以該對象提供的都是用于擷取資料的get方法
● 擷取任意類型的資料:● 擷取指定類型的資料:getObject(int index); getObject(String columnName);
getString(int index); getString(String columnName);
ResultSet rs = null;
//4,向資料庫發SQL,并擷取代表結果集的ResultSet
String sql = "select id,name,password,email,birthday from users";
rs = st.executeQuery(sql);
//5,取出結果集的資料
rs.afterLast();
rs.previous();
System.out.println("id=" + rs.getObject("id"));
System.out.println("name=" + rs.getObject("name"));
System.out.println("password=" + rs.getObject("password"));
System.out.println("email=" + rs.getObject("email"));
System.out.println("birthday=" + rs.getObject("birthday"));
//或者循環取出所有id
while(rs.next()){
String id = rs.getString(1);//1代表資料庫中表的列數,id在第一列也可以("id")!!!
System.out.println("id = "+id);
}