jdbc链接数据库mysql
生活随笔
收集整理的這篇文章主要介紹了
jdbc链接数据库mysql
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package cn.itcast.demo2;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import org.junit.Test;public class Demo2 {/** 連接數據庫,得到Connection就算成功!* 對數據庫做增、刪、改*/@Testpublic void fun1() throws ClassNotFoundException, SQLException {/** 一、得到Connection* 1. 準備四大參數* 2. 加載驅動類* 3. 得到Connection*/// 準備四大參數String driverClassName = "com.mysql.jdbc.Driver";// jdbc協議的格式!jdbc:工商的名稱:子協議(由工商自己來規定)// 對mysql而言,它的子協議結構://主機:端口號/數據庫名稱String url = "jdbc:mysql://localhost:3306/mydb3";String username = "root";String password = "123";// 加載驅動類
Class.forName(driverClassName);// 使用DriverManager,以及省下的3個參數,得到ConnectionConnection con = DriverManager.getConnection(url, username, password);/** 二、對數據庫做增、刪、改* 1. 通過Connection對象創建Statement* > Statement語句的發送器,它的功能就是向數據庫發送sql語句!* 2. 調用它的int executeUpdate(String sql),它可以發送DML、DDL*/// 1. 通過Connection得到Statement對象Statement stmt = con.createStatement();// 2. 使用Statement發送sql語句!
// String sql = "INSERT INTO stu VALUES('ITCAST_0003', 'wangWu', 88, 'male')";
// String sql = "UPDATE stu SET name='zhaoLiu', age=22, " +
// "gender='female' WHERE number='ITCAST_0003'";String sql = "DELETE FROM stu";int r = stmt.executeUpdate(sql);System.out.println(r);}/*** 執行查詢* @throws ClassNotFoundException * @throws SQLException */@Testpublic void fun2() throws ClassNotFoundException, SQLException {/** 一、得到Connection* 二、得到Statement,發送select語句* 三、對查詢返回的“表格”進行解析!*//** 一、得到連接* 1. 準備四大連接參數*/String driverClassName = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/exam";String username = "root";String password = "123";/** 2. 加載驅動類*/Class.forName(driverClassName);/** 3. 通過省下的三個參數調用DriverManger的getConnection(),得到連接*/Connection con = DriverManager.getConnection(url, username, password);/** 二、得到Statement,執行select語句* 1. 得到Statement對象:Connection的createStatement()方法*/Statement stmt = con.createStatement();/** 2. 調用Statement的ResultSet rs = executeQuery(String querySql)*/ResultSet rs = stmt.executeQuery("select * from emp");/** 三、解析ResultSet* 1. 把行光標移動到第一行,可以調用next()方法完成!*/while(rs.next()) {//把光標向下移動一行,并判斷下一行是否存在!int empno = rs.getInt(1);//通過列編號來獲取該列的值!String ename = rs.getString("ename");//通過列名稱來獲取該列的值double sal = rs.getDouble("sal");System.out.println(empno + ", " + ename + ", " + sal);}/** 四、關閉資源* 倒關*/rs.close();stmt.close();con.close();//這個東東,必須要關,不關就死!
}// 規范化
@Testpublic void fun3() throws Exception {Connection con = null;//定義引用Statement stmt = null;ResultSet rs = null;try {/** 一、得到連接*/String driverClassName = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/exam";String username = "root";String password = "123";Class.forName(driverClassName);con = DriverManager.getConnection(url, username, password);//實例化/** 二、創建Statement*/stmt = con.createStatement();String sql = "select * from emp";rs = stmt.executeQuery(sql);rs.last();//把光標移動到最后一行
System.out.println(rs.getRow());rs.beforeFirst();/** 三、循環遍歷rs,打印其中數據* * getString()和getObject()是通用的!*/
// while(rs.next()) {
// System.out.println(rs.getObject(1) + ", "
// + rs.getString("ename") + ", " + rs.getDouble("sal"));
// }int count = rs.getMetaData().getColumnCount();while(rs.next()) {//遍歷行for(int i = 1; i <= count; i++) {//遍歷列
System.out.print(rs.getString(i));if(i < count) {System.out.print(", ");}}System.out.println();}} catch(Exception e) {throw new RuntimeException(e);} finally {// 關閉if(rs != null) rs.close();if(stmt != null) stmt.close();if(con != null) con.close();}}
}
?
轉載于:https://www.cnblogs.com/xiaoxiao5ya/p/4892146.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的jdbc链接数据库mysql的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spark任务调度流程及调度策略分析
- 下一篇: [JavaWeb基础] 025.JAVA