web和mysql连接并增删改查_Java Web 使用IDEA对mysql数据库进行简单增删改查操作(附源码下载)...
一、加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
1.下載jdbc(如已下載可略過(guò)此步驟)
(1)點(diǎn)擊下載網(wǎng)址,選擇Connector/J
(2)選擇Platform Independent版本
(3)下載下圖所示的壓縮包
2.將下載的jar包復(fù)制到WEB-INF下新建的lib中。
3.導(dǎo)入jar包
(1)選擇File下的Project Structure
(2)然后按如下圖所示步驟添加jar包
二、創(chuàng)建Mysql數(shù)據(jù)庫(kù)
1.創(chuàng)建數(shù)據(jù)庫(kù)
打開(kāi)cmd命令行,輸入命令:
create database XXX;//建立數(shù)據(jù)庫(kù)
use XXX;//進(jìn)入數(shù)據(jù)庫(kù)
create table XX(testinfor varchar(30));//創(chuàng)建表
Insert into XX value(’’);//插入數(shù)據(jù)
2.使用可視化工具(例如Navicat)
(1)點(diǎn)擊文件,新建連接選擇MySQL
(2)設(shè)置連接名稱和密碼即可
(3)創(chuàng)建數(shù)據(jù)庫(kù),右鍵點(diǎn)擊一個(gè)自動(dòng)創(chuàng)建的數(shù)據(jù)庫(kù),選擇新建數(shù)據(jù)庫(kù),設(shè)置數(shù)據(jù)庫(kù)名、字符集、排序規(guī)則
(4)新建表,設(shè)置字段名和表名
三、連接數(shù)據(jù)庫(kù)與數(shù)據(jù)庫(kù)可視化
1.連接數(shù)據(jù)庫(kù)
(1)寫下如下代碼:
package?sql;
import?java.sql.*;
public
class
HandleSql?{
//創(chuàng)建?HandleSql?類
static?Connection?con;
//聲明?Connection?對(duì)象
static?PreparedStatement?pStmt;
//聲明預(yù)處理?PreparedStatement?對(duì)象
static?ResultSet?res;
//聲明結(jié)果?ResultSet?對(duì)象
static?String?url?=
"jdbc:mysql://localhost:3307/student?serverTimezone=UTC";
static?String?user?=
"root";
static?String?password?=
"123456";
public?Connection?getConnection(){
//建立返回值為?Connection?的方法
//加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)類
try?{
Class.forName(
"com.mysql.cj.jdbc.Driver");
System.
out.println(
"數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載成功");
}
catch?(ClassNotFoundException?e)?{
e.printStackTrace();
}
//通過(guò)訪問(wèn)數(shù)據(jù)庫(kù)的URL獲取數(shù)據(jù)庫(kù)連接對(duì)象
try?{
con?=?DriverManager.getConnection(url,user,password);
System.
out.println(
"數(shù)據(jù)庫(kù)連接成功");
}
catch?(SQLException?e)?{
e.printStackTrace();
}
return?con;
}
public?static?void?main(String[]?args){
//主方法
HandleSql?h?=
new?HandleSql();
//創(chuàng)建本類對(duì)象
con?=?h.getConnection();
//與數(shù)據(jù)庫(kù)建立連接
}
(2)運(yùn)行代碼,結(jié)果如下圖顯示
2.使用IDEA完成數(shù)據(jù)庫(kù)可視化
(1)點(diǎn)擊View在Tool Windows中選擇Database打開(kāi)數(shù)據(jù)庫(kù)工具窗口
(2)在右側(cè)的窗口中點(diǎn)擊按鈕“+”,選擇數(shù)據(jù)庫(kù)類型(以MySQL為例)
(3)填寫數(shù)據(jù)庫(kù)連接的主機(jī)名、數(shù)據(jù)庫(kù)名、用戶名和密碼
(4)可點(diǎn)擊Test Connection按鈕進(jìn)行測(cè)試數(shù)據(jù)庫(kù)連接是否正常,如下圖所示即為連接正常
四、使用IDEA完成對(duì)數(shù)據(jù)庫(kù)的增刪改查
1.增加(在主方法調(diào)用函數(shù))
public?static?void?addData(){
try?{
pStmt?=?con.prepareStatement(
"insert?into?student?(name,bianhao)?values(?,?)");
pStmt.setString(
1,
"張九");
pStmt.setString(
2,
"0000009");
pStmt.executeUpdate();
}
catch?(Exception?e)?{
e.printStackTrace();
}
}
增加前
增加后
2.刪除(在主方法調(diào)用函數(shù))
public?static?void?deleteData(){
try?{
Statement?stmt?=?con.createStatement();
//創(chuàng)建Statement對(duì)象
stmt.executeUpdate(
"delete?from?student?where?id=4");
stmt.executeUpdate(
"delete?from?student?where?id=5");
}
catch?(Exception?e)?{
e.printStackTrace();
}
}
刪除前
刪除后
3.更新數(shù)據(jù)(在主方法調(diào)用函數(shù))
public?static?void?updateData(){
try?{
pStmt?=?con.prepareStatement(
"update?student?set?name?=???where?id?=?1");
pStmt.setString(
1,
"帥哥");
pStmt.executeUpdate();
}
catch?(Exception?e)?{
e.printStackTrace();
}
}
更新前
更新后
4.查詢數(shù)據(jù)(在主方法調(diào)用函數(shù))
public?static?void?queryData(){
try?{
pStmt=con.prepareStatement(
"select?*?from?student");
res?=?pStmt.executeQuery();
while?(res.next())?{
//如果當(dāng)前語(yǔ)句不是最后一條,則進(jìn)入循環(huán)
int?id?=?res.getInt(
"id");
String?name?=?res.getString(
"name");
String?phone?=?res.getString(
"bianhao");
System.
out.println(
"id:"?+?id?+
"??"?+
"姓名:"?+?name?+
"??"?+
"電話:"?+?phone);
}
}
catch?(Exception?e)?{
e.printStackTrace();
}
}
結(jié)果如下圖所示
總結(jié)
以上是生活随笔為你收集整理的web和mysql连接并增删改查_Java Web 使用IDEA对mysql数据库进行简单增删改查操作(附源码下载)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: common pool2 mysql_用
- 下一篇: 使用url连接mysql时的属性_MyS