五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

通過JDBC訪問Mysql

2023-06-12 10:27 作者:限量版范兒  | 我要投稿

1.JDBC訪問Mysql示例

查詢語句

public class JdbcFirstDemo { ? ?public static void main(String[] args) throws ClassNotFoundException, SQLException { ? ? ? ?// 1.載入jdbc驅動 ? ? ? ?Class.forName("com.mysql.cj.jdbc.Driver"); ? ? ? ?// 2.配置基本信息 ? ? ? ?String url = "jdbc:mysql://localhost:3306/workdb?useUnicode=true&characterEncoding=utf8&useSSL=true"; ? ? ? ?String username = "root"; ? ? ? ?String password = "Wcj920615*"; ? ? ? ?// 3.獲取數(shù)據(jù)庫連接對象 ? ? ? ?Connection connection = DriverManager.getConnection(url, username, password); ? ? ? ?// 4.創(chuàng)建sql對象 ? ? ? ?Statement statement = connection.createStatement(); ? ? ? ?// 5.執(zhí)行查詢sql ? ? ? ?String sql = "select * from t_students"; ? ? ? ?ResultSet resultSet = statement.executeQuery(sql); ? ? ? ?while (resultSet.next()) { ? ? ? ? ? ?System.out.println(resultSet.getObject("id")); ? ? ? ? ? ?System.out.println(resultSet.getObject("name")); ? ? ? ? ? ?System.out.println(resultSet.getObject("gender")); ? ? ? ? ? ?System.out.println(resultSet.getObject("grade")); ? ? ? ? ? ?System.out.println(resultSet.getObject("score")); ? ? ? ? ? ?System.out.println("===================================="); ? ? ? ?} ? ?} }

結果:

插入語句

? ? ? ?// 6.插入語句 ? ? ? ?String sql2 = "insert into t_students (`name`, `gender`, `grade`,`score`,`student_no`) values ('小明','2','4',95,6)"; ? ? ? ?int res = statement.executeUpdate(sql2); ? ? ? ?if (res >= 1) { ? ? ? ? ? ?System.out.println("insert successful."); ? ? ? ?} else { ? ? ? ? ? ?System.out.println("error."); ? ? ? ?}

截圖:

更新語句

? ? ? ?// 7. 更新語句 ? ? ? ?String sql3 = "update t_students set score = 100 where id = 21"; ? ? ? ?int res = statement.executeUpdate(sql3); ? ? ? ?if (res >= 1) { ? ? ? ? ? ?System.out.println("update successful."); ? ? ? ?} else { ? ? ? ? ? ?System.out.println("error."); ? ? ? ?}

截圖:

刪除語句

? ? ? ?// 8. 刪除語句 ? ? ? ?String sql4 = "delete from t_students where id = 21"; ? ? ? ?int res = statement.executeUpdate(sql4); ? ? ? ?if (res >= 1) { ? ? ? ? ? ?System.out.println("delete successful."); ? ? ? ?} else { ? ? ? ? ? ?System.out.println("error."); ? ? ? ?}

截圖:

原文鏈接:https://www.dianjilingqu.com/759245.html

通過JDBC訪問Mysql的評論 (共 條)

分享到微博請遵守國家法律
师宗县| 诸城市| 平和县| 衡东县| 盖州市| 禄劝| 淮阳县| 浦江县| 邹城市| 定西市| 会理县| 抚顺市| 达拉特旗| 巴东县| 宜黄县| 宣威市| 云和县| 山东省| 井冈山市| 大埔区| 和平区| 平武县| 宣威市| 东港市| 贵阳市| 秦皇岛市| 新建县| 克什克腾旗| 嘉兴市| 吐鲁番市| 大姚县| 察隅县| 武隆县| 高碑店市| 邛崃市| 湟源县| 中江县| 蒙自县| 呼图壁县| 大洼县| 当雄县|