需提前导入驱动包
package com.admin; import java.sql.*; public class Main { public static void main(String[] args) throws SQLException { String user = "root"; String password = "123456"; String url = "jdbc:mysql://101.132.67.199:3306/agp"; String driver = "com.mysql.jdbc.Driver"; Connection con = null; //一个封装了TCP长连接 的 数据库长连接对象 Statement stmt = null; //一个封装和管理SQL语句的java对象 ResultSet rSet = null; //一个封装了数据对象 的 无序集合对象 try { Class.forName(driver); //加载数据库驱动到JVM中,并实例化为Driver对象 con = DriverManager.getConnection(url, user, password); //建立TCP数据库长连接,获取Connection对象 con.setAutoCommit(true); stmt = con.createStatement(); //获取SQL管理对象Statement rSet = stmt.executeQuery("select * from user where u_id = 53"); while (rSet.next()) { System.out.println(rSet.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (rSet != null) rSet.close(); if (con != null) con.close(); if (stmt != null) stmt.close(); } } }