1 package com.imooc.db; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 11 12 public class DBUtil { 13 private static final String URL="jdbc:mysql://127.0.0.1:3306/test"; 14 private static final String USER="root"; 15 private static final String PASSWORD="****"; 16 public static void main(String[] args)throws Exception{ 17 18 //1.加载驱动程序 19 Class.forName("com.mysql.jdbc.Driver"); 20 //2.获得数据库的连接 21 Connection conn=DriverManager.getConnection(URL, USER, PASSWORD); 22 23 Statement stmt = conn.createStatement(); 24 ResultSet rs= stmt.executeQuery("select * from tb3"); 25 float sum=0; 26 while(rs.next()) 27 { 28 sum +=rs.getFloat("salary"); 29 System.out.println(rs.getString("username")); 30 } 31 System.out.println(sum); 32 33 34 } 35 }