天貓 99 划算節現金紅包 8 日加碼:狂歡預熱最後一天
1、建立專案
jar包下載地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java?cf_chl_captcha_tk=pmd_0Hlhf0sFkSa0kuykrBJ9W.Ep8tawCh0cl7Gr72sG8fo-1631537827-0-gqNtZGzNAxCjcnBszQiR
1.1、將jdbc的jar包匯入到lib資料夾下
1.2、將jar包新增到專案的庫中
1.3、jdbc連線資料庫步驟
import javax.xml.transform.Result; import java.lang.*; import java.sql.*; public class jdbcDemo1 { public static void main(String[] args) { try{ //1.載入驅動 Class.forName("com.mysql.cj.jdbc.Driver"); //2.使用者資訊和url String url="jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true"; String userName="root"; String password="951731"; //3.連線資料庫 connection代表資料庫物件 Connection connection=DriverManager.getConnection(url,userName,password); //4.建立statement物件來執行sql物件 Statement st=connection.createStatement(); String sql="select * from users"; //5.ResultSet物件用來存放sql語句執行的結果 ResultSet resultSet=st.executeQuery(sql); //如果是查詢操作用executeQuery(sql)方法,如果是增刪改操作統一為executeUpdate(sql)方法,execute()方法能執行任何語句 while(resultSet.next()){ //ResultSet物件指標只想第一條資料每next一次指標下移 System.out.print(resultSet.getInt("id")); //如果知道查詢的欄位型別就用get對應欄位型別,如果不知道則可都用getObject() System.out.print(resultSet.getString("name")); System.out.print(resultSet.getString("password")); System.out.print(resultSet.getString("email")); System.out.print(resultSet.getDate("birthday")); System.out.println(); } //6.釋放連線 resultSet.close(); st.close(); connection.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException sq){ sq.getErrorCode(); } } }
1.4、ResultSet物件的常用方法
1 resultSet.beforeFirst(); //指標移動到最前面
2 resultSet.afterLast(); //指標移動到最後面
3 resultSet.next(); //指標移到下一個資料
4 resultSet.previous(); //指標移到前一行
5 resultSet.absolute(row); //指標移動到指定的第row行
2.PrepareStatement方法
以上方法用的是Statement物件來執行sql語句,然而這種方法並不安全,為解決這種問題,可以使用PreparedStatement介面實現對資料庫表內容的增刪改操作,在現實的開發中也常用PreparedStatement介面而不是Statement介面進行增刪改操作:使用PreparedStatement對於程式碼的可維護性和可讀性提高了;使用PreparedStatement盡最大可能提高效能;最重要的一點是極大地提高了安全性。可以防止資料庫的注入問題。
PreparedStatement介面繼承了Statement的所有功能。另外它還整合一整套getXXX()和setXXX()方法,用於對值得獲取和輸入設定。同時,它還修改了三個方法execute、executeQuery、executeUpdate使它們不需要引數。這些方法的Statement形式,不應該再用於PreparedStatement物件。
PreparedStatement中的方法摘要:
1、executeQuery():在此PreparedStatement物件中執行SQL語句,並返回該查詢生成的ResultSet物件。
2、executeUpdate():在此PreparedStatement物件中執行SQL語句,該語句必須是一個SQL資料操作語言(Date Manipulation Language,DML)語句,比如
insert、update、delete語句;或者是無返內容的SQL語句,比如DDL語句。
3、execute():在此PreparedStatement物件中執行SQL語句,該語句可以是任何種類的SQL語句。
package com.zhao.les4;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
public class jd {
public static void main(String[] args) {
try{
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&";
String userName="root";
String password="951731";
Connection connection= DriverManager.getConnection(url,userName,password);
//sql語句中要輸入的值用佔位符?代替。在建立完PrepareStatement物件後用set???()方法傳入
String sql="insert into users(id,name,password,email,birthday) values(?,?,?,?,null)";
//與Statement物件不同,PrepareStatement需要在建立物件時傳入sql語句,對sql語句進行預編譯
PreparedStatement pstm=connection.prepareStatement(sql);
pstm.setInt(1,6); //第一個引數代表上邊sql語句的第幾個需要傳入的值,第二個引數代表傳入的值
pstm.setString(2,"nidie");
pstm.setString(3,"999999");
pstm.setString(4,"[email protected]");
int i=pstm.executeUpdate();
if(i>0){
System.out.println("插入成功");
}else{
System.out.println("插入失敗");
}
String sql2="select * from users";
PreparedStatement pstm2=connection.prepareStatement(sql2);
ResultSet rs=pstm2.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("password"));
System.out.println(rs.getString("email"));
System.out.println(rs.getDate("birthday"));
}
rs.close();
pstm2.close();
pstm.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
//注意sql語句一定檢查是否正確,本人就因為sql語句錯誤而找了好久錯誤