indelliJ IDEA 查詢數據庫數據(傳統開發方式)及indelliJ IDEA連接數據庫遇到的問題和解決方法

一、創建實體類domain
package cn.tx.domain;

import java.io.Serializable;

//賬戶
public class Account implements Serializable {
    private  Integer id;
    private String name;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }


}
二、創建Dao類操作數據庫

1、創建一個接口

package cn.tx.dao;

import cn.tx.domain.Account;

import java.util.List;

public interface AccountDao {
    public List<Account> findAll();
}

2、實現接口

package cn.tx.dao;

import cn.tx.domain.Account;
import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AccountDaoImpl implements AccountDao{
    public List<Account> findAll() {
        //使用JDBC的程序
        //創建連接池對象,設置4個參數
        DruidDataSource dataSource=new DruidDataSource();
        //設置4個參數
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        //定義對象
        Connection conn =null;
        PreparedStatement stmt=null;
        ResultSet rs =null;
        List<Account> list=new ArrayList<>();


        try {
            //獲取到連接
            conn =dataSource.getConnection();
            //編寫sql
            String sql="select * from account";
            //預編譯
            stmt=conn.prepareStatement(sql);
            //執行sql
            rs = stmt.executeQuery();
            //遍歷結果
            while (rs.next()){
                //進行數據封裝
                Account account=new Account();
                account.setId(rs.getInt("id"));
                account.setName(rs.getString("name"));
                account.setMoney(rs.getDouble("money"));

                //存儲
                list.add(account);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return list;
    }
}
三、創建一個業務層Service類

1、創建一個接口

package cn.tx.service;

import cn.tx.domain.Account;

import java.util.List;

public interface AccountService {
    public List<Account> findAll();
}

2、實現接口

package cn.tx.service;

import cn.tx.dao.AccountDao;
import cn.tx.dao.AccountDaoImpl;
import cn.tx.domain.Account;

import java.util.List;

public class AccountServiceImpl implements AccountService{
    public List<Account> findAll() {
        //創建Dao對象
        AccountDao accountDao=new AccountDaoImpl();
        return accountDao.findAll();
    }
}
四、測試類
package cn.tx.test;

import cn.tx.domain.Account;
import cn.tx.service.AccountService;
import cn.tx.service.AccountServiceImpl;
import org.junit.Test;

import java.util.List;

public class Demo1 {

    /*第一種方式,new對象的方式查詢數據*/
    @Test
    public void run1(){
        //創建對象
        AccountService accountService=new AccountServiceImpl();
        //調用方法
        List<Account> list= accountService.findAll();
        //遍歷
        for (Account account:list){
            System.out.println(account);
        }

    }
}
五、運行查詢數據庫account表的數據結果如下:

在這裏插入圖片描述

完成了。

但是,運行之前我遇到了一些問題:
1、數據庫連接不成功:

IDEA連接mysql報錯!Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ prope
翻譯:服務器返回無效時區。進入“高級”選項卡,手動設置“serverTimezone”屬性。

解決:
1、檢查你的mysql是否已經配置完環境變量(如果你是剛剛下載mysql還沒配置環境變量的可以去查查怎麼樣配置)
2、配置完環境變量後 進入命令窗口(Win + R),連接數據庫 mysql -hlocalhost -uroot -p,回車,輸入密碼,回車,如圖:
在這裏插入圖片描述
2、繼續輸入 show variables like’%time_zone’; (注意不要漏掉後面的分號),回車,如圖:
在這裏插入圖片描述
3、設置時區。

輸入set global time_zone = ‘+8:00’; 注意不要漏掉後面的分號),回車,如圖:
在這裏插入圖片描述
設置成功,重新連接下數據庫,連接成功!
在這裏插入圖片描述
以上就是我遇到的問題和解決的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章