數據庫基礎
?SQL語言
1、select?語句
select 語句用于從數據中檢索數據。語法如下:
SELECT 搜選字段列表 FROM?數據表名
WHERE?條件表達式?GROUP BY 字段名 HAVING 條件表達式(指定分組的條件)
ORDER BY 字段名[ASC|DESC]
2、insert 語句
insert 語句用于向表中插入新數據。語法如下:
insert into 表名[(字段1,字段2...)]values(屬性值1,屬性值2);
?
?3、update 語句
update 語句用于更新數據表中的某些記錄。語法如下:
UPDATE 數據表名 SET 字段名=新的字段值 WHERE 條件表達式;
?
?4、delete 語句
delete 語句用于刪除數據。語法如下:
delete from 數據表名 where 條件表達式;
?
?
DBC中常用的類和接口
1、 DriverManager 類
??DriverManager 類師JDBC的管理層,用于管理數據庫中的驅動程序。在操作指定數據庫之前,需要使用Java中Class 類的靜態方法forName(String className)加載指定數據庫的驅動程序。
2、Connection 接口
?? Connection 接口代表與特定的數據庫的連接,在連接上下文中執行SOL語句并返回結果。?
3、Statement 接口
? ? ? Statement 接口用于在已經建立連接的基礎上向數據庫發送SQL語句。?
4、PreparedStatement 接口
? ? ?PreparedStatement 接口用來動態地執行SQL語句。通過PreparedStatement 實例執行的動態的SQL語句,將被預編譯并能保存到PreparedStatement 實例中,從而可以反復地執行該SQL語句。?
5、ResultSet 接口
? ? ? ?ResultSet 接口類似與一個臨時表,用來展示存放數據庫查詢操作所獲得的結果。ResultSet 實例具有指定當前數據行的指針,指針開始的位置在第一個記錄的前面,通常next()方法可將指針向下移。?
數據庫操作
? ? ? 要對數據庫表中的數據庫進行操作,首先應該建立與數據庫的連接。通過JDBC API 中提供的各種各類,可對數據表中的數據進行查找、添加、修改、刪除等操作。代碼如下:
?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import com.mysql.cj.jdbc.Driver;public class JDBC {Connection connection;public static void main(String[] args)throws SQLException {JDBC jdbc=new JDBC();jdbc.getConnection();System.out.println("查詢所有課程,結果為:");jdbc.selectAll();System.out.println("新增4號課程,名為Web");jdbc.add(4,"Web");System.out.println("查詢所有課程,結果為:");jdbc.selectAll();System.out.println("修改三課程,名為office");jdbc.update(3,"office");System.out.println("查詢所有課程,結果為:"); jdbc.selectAll(); System.out.println("刪除4號課程");jdbc.delete(4);System.out.println("查詢所有課程,結果為:");jdbc.selectAll();jdbc.close();}//刪除課程public void delete(int id) throws SQLException {//第三步:獲取statement對象PreparedStatement preparedStatement=connection.prepareStatement("delete from couse where id= ?;");preparedStatement.setInt(1,id);//執行SQL語句返回結果集preparedStatement.executeUpdate();}//修改課程public void update(int id,String name) throws SQLException {//第三步:獲取statement對象PreparedStatement preparedStatement=
connection.prepareStatement("update couse set name= ? where id= ?;");preparedStatement.setString(1, name);preparedStatement.setInt(2, id);//第四步: 獲取statement對象preparedStatement.executeUpdate();}//添加課程public void add(int id, String name)throws SQLException {//第三步:獲取statement對象PreparedStatement preparedStatement=connection.prepareStatement("insert into couse value(?,?);");preparedStatement.setInt(1, id);preparedStatement.setString(2, name);//第四步:執行SQL語句返回結果集preparedStatement.executeUpdate();}public void getConnection()throws SQLException{//第一步注冊驅動 DriverManager.registerDriver(new Driver()); //第二步:獲取連接 connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/school_java","root","1234");}//查詢所有課程public void selectAll()throws SQLException {//第三步:獲取statement對象PreparedStatement preparedStatement=connection.prepareStatement("select * from couse;");
//第四步: 執行SQL語句返回結果集ResultSet resultSet=preparedStatement.executeQuery();
//第五步:遍歷結果集while(resultSet.next()) {System.out.print(resultSet.getInt("id")+" ");System.out.println(resultSet.getString("name"));}//第六步:關閉連接釋放資源resultSet.close();preparedStatement.close();}public void close()throws SQLException {connection.close();}
}
運行結果:
?
?
?
?