生活随笔
收集整理的這篇文章主要介紹了
Java连接Mysql数据库增删改查实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java連接Mysql數(shù)據(jù)庫增刪改查實現(xiàn)
時間比較趕,我這里只實現(xiàn)查詢,有時間再添加另外兩個
難度 : ???(全星5顆星的情況下)
新建一個動態(tài)的網(wǎng)站工程,
把jar包全部復(fù)制進去,主要要那個mysql-connector-java那個jar包,重要通知,我的mysql版本是5.7.18-log版本,這個jar版本夠,如果你裝的是mysql8點幾的版本,則帶不起來,需要高版本的驅(qū)動版;
package com
.anhui
.dajun
;import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;public class JDBCUtil {private static final String URL
= "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";private static final String DRIVERS
= "com.mysql.jdbc.Driver";private static final String USERNAME
= "root";private static final String PASSWORRD_STRING
= "root";public static void main(String
[] args
) {Connection connection
= null
;ResultSet rs
= null
;Statement stmt
= null
;try {try {Class
.forName(DRIVERS
);connection
= DriverManager
.getConnection(URL
, USERNAME
, PASSWORRD_STRING
);stmt
= connection
.createStatement();String sql
= "SELECT * FROM tt_user";rs
= stmt
.executeQuery(sql
);while (rs
.next()) {System
.out
.println("Id 是 : " + rs
.getString("id") + " | " + "姓名 : " + rs
.getString("name") + " | "+ "年齡 : " + rs
.getString("age") + " | " + " 地址 : " + rs
.getString("address"));System
.out
.println("--------------------------------");}} catch (SQLException e
) {e
.printStackTrace();} catch (ClassNotFoundException e
) {e
.printStackTrace();}} finally {if (rs
!= null
) {try {rs
.close();} catch (SQLException e
) {e
.printStackTrace();}}if (stmt
!= null
) {try {stmt
.close();} catch (SQLException e
) {e
.printStackTrace();}}if (connection
!= null
) {try {connection
.close();} catch (SQLException e
) {e
.printStackTrace();}}}}}
;create table `tt_user
` (`id
` int ,`name
` varchar ,`age
` int ,`address
` varchar
);
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('1','大軍','26','上海市');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('2','楊紅','24','云南');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('3','趙劉','27','陜西');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('4','李四','23','安徽');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('5','劉六','30','安徽安慶');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('6','王二','22','北京市');
insert into `tt_user
` (`id
`, `name
`, `age
`, `address
`) values('7','123','213','213');
總結(jié)
以上是生活随笔為你收集整理的Java连接Mysql数据库增删改查实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。