1.原生態(tài)連接
①:準備工作:引入mysql依賴:
? ? ? <dependency>
? ? ? ? ? ?<groupId>mysql</groupId>
? ? ? ? ? ?<artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ?<version>5.1.6</version>
? ? ? ?</dependency>
②:書寫DBUtil工具類:全代碼
import java.sql.*;
?
public class DBUtil {
? public static final String username="root";//連接數(shù)據(jù)庫的用戶名
? public static final String password="***";//連接數(shù)據(jù)庫的密碼
? public static final String url="jdbc:mysql://localhost:3306/db02?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8";//url的路徑
? public static Connection getCon() throws SQLException {
? ? ? try {
? ? ? ? ? Class.forName("com.mysql.jdbc.Driver");
? ? ? } catch (ClassNotFoundException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? ? ? return DriverManager.getConnection(url,username,password);
? }
? public static Statement getStatement(Connection connection) throws SQLException {
? ? ? return connection.createStatement();
? }
? public static ResultSet getResultSet(Statement statement,String sql) throws SQLException {
? ? ? return statement.executeQuery(sql);
? }
}
?
③:連接數(shù)據(jù)庫的四大步驟
1>:加載驅動
2>:獲取連接
public static Connection getCon() throws SQLException {
? ? ? ?try {
? ? ? ? ? ?Class.forName("com.mysql.jdbc.Driver"); ? //1.加載驅動
? ? ? } catch (ClassNotFoundException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? }
? ? ? ?return DriverManager.getConnection(url,username,password); ? ? ? //2.獲取連接
? }
3>:通過你的連接來獲取操作數(shù)據(jù)庫的statement對象
public static Statement getStatement(Connection connection) throws SQLException ? {
? ? ? ?return connection.createStatement();//注:prepareStatement()可以防止SQL注入問題
? }
4>:執(zhí)行sql語句,獲取結果集
public static ResultSet getResultSet(Statement statement,String sql) throws SQLException {
? ? ? ?return statement.executeQuery(sql);
? }
④:代碼測試
1>:數(shù)據(jù)庫表的設計:
user表:
?
2>:在SpringBoot的測試單元經(jīng)行測試
@SpringBootTest
class MysqlApplicationTests {
?
? ?@Test
? ?void contextLoads() throws SQLException {
? ? ? ?Connection connection = DBUtil.getCon();//驅動加載和連接
? ? ? ?Statement statement = DBUtil.getStatement(connection);//得到statement對象
? ? ? ?ResultSet resultSet = DBUtil.getResultSet(statement, "select * from db02.user");//執(zhí)行CRUD的sql語句得到結果集
? ? ? ?if(resultSet!=null){
? ? ? ? ? ?while (resultSet.next()){//遍歷結果集,打印查詢結果
? ? ? ? ? ? ? ?String id = resultSet.getString(1);
? ? ? ? ? ? ? ?String username=resultSet.getString(2);
? ? ? ? ? ? ? ?String password=resultSet.getString(3);
? ? ? ? ? ? ? ?String email = resultSet.getString(4);
? ? ? ? ? ? ? ?String sex = resultSet.getString(5);
? ? ? ? ? ? ? ?String age = resultSet.getString(6);
? ? ? ? ? ? ? ?System.out.println(id+" "+username+" "+password+" "+email+" "+sex+" "+age);
? ? ? ? ? ? ? ?System.out.println("===============");
? ? ? ? ? }
? ? ? }
? }
?
}
3>:測試結果
?
2.在yml或則properties文件里,直接配置
以yml文件為例:文章來源:http://www.zghlxwxcb.cn/news/detail-528857.html
spring:
?datasource:
? ?url: jdbc:mysql://localhost:3306/db02?ServerTimezone=UTC
? ?username: root ?
? ?password: ***
? ?driver-class-name: com.mysql.cj.jdbc.Driver
3.總結
以上就是關于自己總結的連接mysql數(shù)據(jù)庫的兩種方法,實際的操作也不難,希望可以幫助到大家!文章來源地址http://www.zghlxwxcb.cn/news/detail-528857.html
到了這里,關于Mysql的數(shù)據(jù)庫連接---SpringBoot的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!