jdbc建立数据库连接的helloword

package com.guoguo.db;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DBUtil {

//数据库连接地址
private static final String URL = "jdbc:mysql://127.0.0.1:3306/spring_test";
private static final String USER = "root";
private static final String PASSWORD = "";

public static void main(String[] args) throws ClassNotFoundException,
SQLException {
// TODO 自动生成的方法存根
// 1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 2.获取数据库的连接
java.sql.Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
//3.通过数据库的连接操作数据库
java.sql.Statement stmt=conn.createStatement();
ResultSet result=stmt.executeQuery("select name,money from account");
while (result.next()){
System.out.println(result.getString("name")+","+result.getString("money"));
}
}

}

原文地址:https://www.cnblogs.com/guojie001/p/5650569.html