(2-23)使用jdbc实现web数据存储(一)

笔记如下

1.引入第三方类库


2018-02-23_115807.png

2.实现步骤


(2-23)使用jdbc实现web数据存储(一)_第1张图片
2018-02-23_115838.png
public class JdbcQuickStart {
    
    
    @Test
    public void test1() {
        
        Connection conn = null;
        Statement stmt  = null;
        ResultSet rs  = null;
        try {
            //第一步:注册驱动
            DriverManager.registerDriver(new Driver());
            //建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day15_jdbc","root","123456");
            
            //第三步:获得可以发送sql语句的statement对象
            
            stmt = conn.createStatement();
        
            //第四步:执行sql语句,拿到结果集
            rs = stmt.executeQuery("select * from users");
            
            //第五步:解析结果集的数据
            while(rs.next()) {
                
                int id = rs.getInt("id");
                
                String username = rs.getString("username");
                String password = rs.getString("password");
                String nickname = rs.getString("nickname");
                
                System.out.println("id:  "+id+",username: "+username+",nickname:  "+nickname);
                
                
                
            }
            
            
        } catch (SQLException e) {
            //第六步:释放资源,都会在finally释放
            try {
                conn.close();
                stmt.close();
                rs.close();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        
        
    }
    
}

你可能感兴趣的:((2-23)使用jdbc实现web数据存储(一))