24、jdbc操作数据库(1)
什么是jdbc?
看一下官方怎么說,JDBC 英文名Java DataBase Connectivity,使用java連接數(shù)據(jù)庫的工具,就是一組使用java代碼來執(zhí)行SQL語句的API。
Jdbc有什么用?
數(shù)據(jù)庫有多種,并且不同數(shù)據(jù)庫操作時(shí)的方式和語句可能不太一樣,當(dāng)使用java 代碼控制sql 調(diào)用時(shí),不可能每一種數(shù)據(jù)庫都弄一套 java 的代碼,所以就聲明了一個(gè)套操作數(shù)據(jù)庫的接口,java程序員只需調(diào)用這一套接口即可,具體的接口實(shí)現(xiàn)由對(duì)應(yīng)的數(shù)據(jù)庫廠商實(shí)現(xiàn),所以這里就又有了一個(gè)新的概念叫做數(shù)據(jù)庫驅(qū)動(dòng),這個(gè)驅(qū)動(dòng)就是不同數(shù)據(jù)庫廠商實(shí)現(xiàn) JDBC 接口的類的一個(gè)工具包,當(dāng)時(shí)用jdbc操作數(shù)據(jù)庫時(shí),需要添加對(duì)應(yīng)的數(shù)據(jù)庫驅(qū)動(dòng)包。
如何使用jdbc?
使用jdbc操作數(shù)據(jù)庫有以下幾步:
接下來詳細(xì)的介紹一下
添加驅(qū)動(dòng)并獲取數(shù)據(jù)庫連接。不同的數(shù)據(jù)庫對(duì)應(yīng)的驅(qū)動(dòng)包不一樣,添加驅(qū)動(dòng)包(添加jar包)之后需要通過驅(qū)動(dòng)去獲取一個(gè)數(shù)據(jù)庫連接,沒什么好說的,直接上代碼:
//聲明一個(gè)配置文件jdbc.properties #數(shù)據(jù)庫驅(qū)動(dòng)類 driver=com.mysql.jdbc.Driver #連接數(shù)據(jù)庫 jdbcUrl=jdbc:mysql://127.0.0.1:3306/fqx_webapp #用戶名 user=root #密碼 password=123456--------------------------------------------------------------//工具類(基本類) public class DBUtil {/*** 獲取配置文件信息* @return*/private static Properties getProperties() {InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");Properties properties = new Properties();try {properties.load(inputStream);} catch (IOException e) {e.printStackTrace();}return properties;}/*** 獲取數(shù)據(jù)庫連接 java.sql.Connection的一個(gè)實(shí)例* @return*/public static Connection getConnection() {Connection connection = null;Properties properties = getProperties();String driver = properties.getProperty("driver");try {//加載數(shù)據(jù)庫驅(qū)動(dòng)Class.forName(driver);//使用驅(qū)動(dòng)管理器獲取數(shù)據(jù)庫連接connection = DriverManager.getConnection(properties.getProperty("jdbcUrl"), properties);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return connection;}/*** 釋放資源* @param resultSet* @param statement* @param connection*/public static void close(ResultSet resultSet , Statement statement , Connection connection) {try {if (resultSet != null) {resultSet.close();}if (statement != null) {statement.close();}if (connection != null) {connection.close();}} catch (Exception e) {e.printStackTrace();}}}使用java對(duì)象數(shù)據(jù)庫進(jìn)行簡單的增、刪、改、查操作,代碼如下:
public class Test {public static void main(String[] args) {Test test = new Test(); // test.insert(); // test.delete(); // test.update();test.query();}//添加public void insert() {//獲取數(shù)據(jù)庫連接Connection connection = DBUtil.getConnection();//編寫sql,需要傳參數(shù)的地方使用 ? 來表示,叫做占位符String sql = "INSERT INTO USER (uname , password) VALUES (? , ?)";PreparedStatement prepareStatement = null;try {//通過數(shù)據(jù)庫連接獲取數(shù)據(jù)庫操作對(duì)象prepareStatement = connection.prepareStatement(sql);//使用對(duì)應(yīng)的值填充占位符,key是占位符的下標(biāo),下標(biāo)在1開始prepareStatement.setObject(1, "zhangsan");prepareStatement.setObject(2, 18);//發(fā)送并sql,返回結(jié)果//增刪改可以使用executeUpdate()方法,查詢使用executeQuery()方法int update = prepareStatement.executeUpdate();System.out.println("是否添加成功:"+update);} catch (SQLException e) {e.printStackTrace();} finally {//關(guān)閉資源DBUtil.close(null, prepareStatement, connection);}}//刪除public void delete() {Connection connection = DBUtil.getConnection();String sql = "DELETE FROM USER WHERE ID = ?";PreparedStatement prepareStatement = null;try {prepareStatement = connection.prepareStatement(sql);prepareStatement.setObject(1, 3);int update = prepareStatement.executeUpdate();System.out.println("是否刪除成功:"+update);} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(null, prepareStatement, connection);}}//修改public void update() {Connection connection = DBUtil.getConnection();String sql = "UPDATE USER SET UNAME = ? WHERE ID = ?";PreparedStatement prepareStatement = null;try {prepareStatement = connection.prepareStatement(sql);prepareStatement.setObject(1, "wangwu");prepareStatement.setObject(2 , 2);int update = prepareStatement.executeUpdate();System.out.println("是否修改成功:"+update);} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(null, prepareStatement, connection);}}//查詢public void query() {Connection connection = DBUtil.getConnection();String sql = "SELECT ID,UNAME,PASSWORD FROM USER";PreparedStatement prepareStatement = null;ResultSet resultSet = null;try {prepareStatement = connection.prepareStatement(sql);//獲取到結(jié)果集resultSet = prepareStatement.executeQuery();List<Map<String, Object>> list = new ArrayList<>();//返回的相當(dāng)于一張表,表中有一個(gè)指針,當(dāng)調(diào)用resultSet.next()方法時(shí),指針就往后移一次while(resultSet.next()){Map<String, Object> map = new HashMap<>(); //根據(jù)對(duì)應(yīng)的字段名獲取結(jié)果幾種的值map.put("ID", resultSet.getObject("ID"));map.put("UNAME", resultSet.getObject("UNAME"));map.put("PASSWORD", resultSet.getObject("PASSWORD"));list.add(map);}System.out.println("查詢出的信息:"+list);} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(resultSet, prepareStatement, connection);}}}?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的24、jdbc操作数据库(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上千辆新款特斯拉 Model 3 现身上
- 下一篇: 戴尔 U2424H / HE 显示器开卖