JDBC连接数据库演示案例

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 public class DBUtil {
 8     
 9     private static final String URL="jdbc:mysql://127.0.0.1:3306/student";
10     private static final String USER="root";
11     private static final String PASSWORD="root";
12 
13     public static void main(String[] args) throws ClassNotFoundException, SQLException {
14         
15         //1、加载驱动程序
16         Class.forName("com.mysql.jdbc.Driver");
17         //2、获得数据库的连接
18         Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
19         //3、通过数据的连接来操作数据库,实现增删改查
20         Statement stmt = conn.createStatement();
21         ResultSet rst = stmt.executeQuery("select name,age from stu");
22         while(rst.next()){
23             System.out.println(rst.getString("name")+","+rst.getInt("age"));
24         }
25 
26     }
27 
28 }

 

posted @ 2016-11-15 11:21  Web1024  阅读(400)  评论(0编辑  收藏  举报