java第一次连接数据库

全文参考:https://www.cnblogs.com/Qian123/p/5339164.html#_label0

解决时区报错:https://blog.csdn.net/aayygg1234/article/details/78205240

package jnxyjsj.zs.po;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.jdbc.Driver;
public class Customer {

    public static void main(String[] args) {
        try {
            // 不加?serverTimezone=UTC 会提示时区错误
            Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.2:3306/market?serverTimezone=UTC", "testuser", "wif12!@13..D");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select productID, name, price from product");
            while(rs.next()){//如果对象中有数据,就会循环打印出来
                System.out.println(rs.getString("name")+","+rs.getInt("price"));
            }
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        // TODO 自动生成的方法存根
        //Class.forName("com.mysql.cj.jdbc.Driver");

    }

}

 

posted @ 2020-12-09 15:46  夏秋初  阅读(180)  评论(0编辑  收藏  举报