java jdbc 連接mysq 測試l 雲服務器 和雲數據庫 mysql DB

話很少說直接上效果圖

下面給出參考代碼:

package com.humi.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBTest {
    
//  public  DBTest(){};
//  public  DBTest(String a){};
    
    //ip+port
//  static String URL ="jdbc:mysql://119.29.36.221:3306/test";//服務器數據庫
    static String URL ="jdbc:mysql://1592ba7b93d216.gz.cdb.myqcloud.com:7107/e_market";//騰訊雲數據庫
    //uid
    static String userName="root"; //默認root mysql  沒有去改她
    //pwd
    static String userPassword="***";   //這裏輸入本身的密碼
    //mysql SQL;
    static String SQL="select * from mysql_user";   
    //mysql connection 
    static Connection conn =null;
    //prestatement
    static PreparedStatement pst = null;
    //resultSet 
    static ResultSet rs = null;
    
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驅動加載成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("沒有找到驅動!");
            e.printStackTrace();
        }
    }
    public static void conectDB(String URL,String userName,String userPassword){
        try {
            conn = DriverManager.getConnection(URL, userName, userPassword);
            System.out.println("已鏈接上"+URL+":\n數據庫"+conn);
        } catch (SQLException e) {
            System.out.println("獲取連接失敗");
            e.printStackTrace();
        }
    }
    public static void querryByid(String SQL){
        try {
            pst = conn.prepareStatement(SQL);
            System.out.println("執行查詢語");
            rs = pst.executeQuery();
            while(rs.next()){
                System.out.println("id"+"uname"+"upwd\n"+
                        rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
            }
        } catch (SQLException e) {
            System.out.println("查詢失敗");
            e.printStackTrace();
        }
    }
    public static void close(Connection conn,PreparedStatement pst, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                if(pst!=null){
                    try {
                        pst.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }finally{
                        if(conn!=null){
                            try {
                                conn.close();
                                System.out.println("數據庫連接已關閉,資源已釋放");
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }finally{
                                conn=null;
                                pst= null;
                                rs = null;
                            }
                        }
                    }
                }
            }
        }
    }
    
    public static void main(String[] args) {
        DBTest dbUtil = new DBTest();
        dbUtil.conectDB(URL, userName, userPassword);
        dbUtil.querryByid(SQL);
        dbUtil.close(conn, pst, rs);
    }
}
相關文章
相關標籤/搜索