日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程

發(fā)布時(shí)間:2023/12/15 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Step 1 你得有Eclipse

沒(méi)有出門(mén)右拐,我教不了你。

Step 2 你得有Mysql

MySQL的詳細(xì)安裝過(guò)程,我在另一篇博客中給出。戳我

Step 3 安裝JDBC

可以去官網(wǎng)下,如果用的我的Mysql版本的話,可以直接下我的,我的是.19下載地址
如果不是,給出官網(wǎng)下載地址
有小伙伴私信我說(shuō),不知道那個(gè)是Windows的:我來(lái)解答一下。

1. 導(dǎo)入U(xiǎn)ser Library

直接拖進(jìn)去就可以了





2. 導(dǎo)入用戶自訂的Library



Stept 4 數(shù)據(jù)庫(kù)中來(lái)張表

相信很多道友,Mysql都沒(méi)用過(guò),一直在吃灰。
我們搞張表
打開(kāi)Mysql

//cmd輸入,看過(guò)我之前的博客應(yīng)該會(huì)了就不贅述了 mysql -uroot -p Enter password: create database db;--建立數(shù)據(jù)庫(kù)use db;create table user(id int, name varchar(20));insert into user(id, name) values (1, 'hello');insert into user(id, name) values(2, 'good'); --建表

Stept 5 檢查是否成功

在剛才的工程里

import java.sql.*; public class TEST {public static void main(String[] args) {String driver = "com.mysql.cj.jdbc.Driver"; //加載驅(qū)動(dòng)程序,不用改String username = "root"; //數(shù)據(jù)庫(kù)用戶名按自己的改改!!!!!!!!String password = "";//數(shù)據(jù)庫(kù)密碼按自己的改改!!!!!!!!!!!String Dbname="db";//以后訪問(wèn)自己數(shù)據(jù)庫(kù)的時(shí)候按需修改,測(cè)試先用這個(gè)String url = "jdbc:mysql://localhost:3306/"+Dbname+"?&useSSL=false&serverTimezone=UTC";String coding="&useUnicode=ture&characterEncoding=UTF-8";//編碼格式Connection conn = null;try{Class.forName(driver);//getConnection()方法,連接MySQL數(shù)據(jù)庫(kù)!conn=DriverManager.getConnection(url+coding,username,password);if(!conn.isClosed())System.out.println("-------------------------------數(shù)據(jù)庫(kù)連接成功!---------------------------");//創(chuàng)建statement類(lèi)對(duì)象,用來(lái)執(zhí)行SQL語(yǔ)句!Statement Statement=conn.createStatement();//要執(zhí)行的SQL語(yǔ)句String sql="select * from user" ;//ResultSet類(lèi),用來(lái)存放獲取的結(jié)果集!ResultSet rs=Statement.executeQuery(sql);while(rs.next()){System.out.println(rs.getString("name"));}}catch(ClassNotFoundException e){//數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)異常處理System.out.println("數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載失敗!");e.printStackTrace();}catch(SQLException e1){//數(shù)據(jù)庫(kù)連接失敗異常處理e1.printStackTrace();}catch(Exception e2){e2.printStackTrace();}finally{System.out.println("-------------------------------數(shù)據(jù)庫(kù)數(shù)據(jù)獲取成功!---------------------------");} } }


完成跑路!!!

Step 6 增刪查改模板:

1.增

如果不能成功鏈接數(shù)據(jù)庫(kù),我的博客JAVA中有詳細(xì)的介紹,可以看一下

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; //當(dāng)如相關(guān)驅(qū)動(dòng)包public class Add {public static void main(String[] args) throws Exception {String driver = "com.mysql.cj.jdbc.Driver"; //加載驅(qū)動(dòng)程序,不用改String userName = "root"; //數(shù)據(jù)庫(kù)用戶名按自己的改改!!!!!!!!String userPwd = "";//數(shù)據(jù)庫(kù)密碼按自己的改改!!!!!!!!!!!String Dbname="students";//以后訪問(wèn)自己數(shù)據(jù)庫(kù)的時(shí)候按需修改,測(cè)試先用這個(gè)String url = "jdbc:mysql://localhost:3306/"+Dbname+"?&useSSL=false&serverTimezone=UTC";String coding="&useUnicode=ture&characterEncoding=UTF-8";//編碼格式url = url+coding; // 形成帶數(shù)據(jù)庫(kù)讀寫(xiě)編碼的數(shù)據(jù)庫(kù)連接字Class.forName(driver); // 加載并注冊(cè)驅(qū)動(dòng)程序Connection conn = DriverManager.getConnection(url, userName, userPwd);// 創(chuàng)建連接對(duì)象if (!conn.isClosed())System.out.println("Succeeded connecting to the Database!");//Statement stmt = conn.createStatement();// 現(xiàn)在很少有人用了,部分老師比較古板,所以你不寫(xiě)可能會(huì)扣分的呀。笑哭// 更新(添加、刪除、修改)數(shù)據(jù)庫(kù)操作String sql = "insert into stu(xh,name,cj) values(2,'李四',98)";PreparedStatement pstmt = conn.prepareStatement(sql);int n = pstmt.executeUpdate(sql);// 返回記錄操作條數(shù)if (n > 0) {System.out.println("添加記錄成功,共添加了" + n + "條記錄");} else {System.out.println("添加不成功!");}pstmt.close();// stmt.close();conn.close();}}
2.刪
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement;public class Delete {public static void main(String[] args) throws Exception {String driverName = "com.mysql.cj.jdbc.Driver"; // 驅(qū)動(dòng)程序名String userName = "root"; // 數(shù)據(jù)庫(kù)用戶名String userPwd = null; // 密碼String dbName = "students"; // 數(shù)據(jù)庫(kù)名String url = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&serverTimezone=UTC"; // 形成帶數(shù)據(jù)庫(kù)讀寫(xiě)編碼的數(shù)據(jù)庫(kù)連接字Class.forName(driverName); // 加載并注冊(cè)驅(qū)動(dòng)程序Connection conn = DriverManager.getConnection(url, userName, userPwd);// 創(chuàng)建連接對(duì)象if (!conn.isClosed())System.out.println("Succeeded connecting to the Database!");Statement stmt = conn.createStatement();// 在橋conn上直接創(chuàng)建一輛汽車(chē)String sql = "delete from stu where cj<?"; //用占位符設(shè)置SQL操作的模板PreparedStatement pstmt = conn.prepareStatement(sql); //預(yù)處理相關(guān)SQL語(yǔ)句int n = stmt.executeUpdate(sql);// 返回記錄操作條數(shù)// pstmt.setInt(1,60);if (n > 0) {System.out.println("刪除記錄成功,共刪除了" + n + "條記錄");} else {System.out.println("刪除不成功!");}pstmt.close();stmt.close();conn.close();}}
3.查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;public class Select {public static void main(String[] args) throws Exception {String driverName = "com.mysql.cj.jdbc.Driver"; // 驅(qū)動(dòng)程序名String userName = "root"; // 數(shù)據(jù)庫(kù)用戶名String userPwd = ""; // 密碼String dbName = "students"; // 數(shù)據(jù)庫(kù)名String url = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&serverTimezone=UTC"; // 形成帶數(shù)據(jù)庫(kù)讀寫(xiě)編碼的數(shù)據(jù)庫(kù)連接字Class.forName(driverName); // 加載并注冊(cè)驅(qū)動(dòng)程序Connection conn = DriverManager.getConnection(url, userName, userPwd);// 創(chuàng)建連接對(duì)象if (!conn.isClosed())System.out.println("Succeeded connecting to the Database!");Statement stmt = conn.createStatement();// 在橋conn上直接創(chuàng)建一輛汽車(chē)String sql = "select * from stu where cj>=0 and cj<=100";PreparedStatement pstmt = conn.prepareStatement(sql);ResultSet rs = pstmt.executeQuery();// 執(zhí)行,得到查詢結(jié)果集合System.out.println("記錄號(hào) 學(xué)號(hào) 姓名 成績(jī)");while (rs.next()) {int a = rs.getRow();int b = rs.getInt("xh");String c = rs.getString("name");int d = rs.getInt("cj");System.out.println(a + " | " + b + " | " + c + " | " + d);}pstmt.close();stmt.close();conn.close();}}
4.改
```cppimport java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement;public class Updata {public static void main(String[] args) throws Exception {String driverName = "com.mysql.cj.jdbc.Driver"; // 驅(qū)動(dòng)程序名不需要修改String userName = "root"; // 數(shù)據(jù)庫(kù)用戶名String userPwd = null; // 密碼String dbName = "students"; // 數(shù)據(jù)庫(kù)名String url = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&serverTimezone=UTC"; // 形成帶數(shù)據(jù)庫(kù)讀寫(xiě)編碼的數(shù)據(jù)庫(kù)連接字Class.forName(driverName); // 加載并注冊(cè)驅(qū)動(dòng)程序Connection conn = DriverManager.getConnection(url, userName, userPwd);// 創(chuàng)建連接對(duì)象if (!conn.isClosed())System.out.println("Succeeded connecting to the Database!");Statement stmt = conn.createStatement();// 在橋conn上直接創(chuàng)建一輛汽車(chē)// 更新(添加、刪除、修改)數(shù)據(jù)庫(kù)操作String sql = "update stu set cj=? where xh=3";PreparedStatement pstmt = conn.prepareStatement(sql);int n = stmt.executeUpdate(sql);// 返回記錄操作條數(shù)// pstmt.setInt(1,50); // pstmt.setInt(2,3);if (n > 0) {System.out.println("修改記錄成功,共修改了" + n + "條記錄");} else {System.out.println("修改不成功!");}pstmt.close();stmt.close();conn.close();}}


寫(xiě)在最后:
我叫風(fēng)骨散人,名字的意思是我多想可以不低頭的自由生活,可現(xiàn)實(shí)卻不是這樣。家境貧寒,總得向這個(gè)世界低頭,所以我一直在奮斗,想改變我的命運(yùn)給親人好的生活,希望同樣被生活綁架的你可以通過(guò)自己的努力改變現(xiàn)狀,深知成年人的世界里沒(méi)有容易二字。目前是一名在校大學(xué)生,預(yù)計(jì)考研,熱愛(ài)編程,熱愛(ài)技術(shù),喜歡分享,知識(shí)無(wú)界,希望我的分享可以幫到你!
如果有什么想看的,可以私信我,如果在能力范圍內(nèi),我會(huì)發(fā)布相應(yīng)的博文!
謝謝大家的閱讀!😘

總結(jié)

以上是生活随笔為你收集整理的学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。