码迷,mamicode.com
首页 > 数据库 > 详细

2014=9=24 连接数据库2

时间:2014-09-24 23:49:17      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   java   ar   for   

package cn.sql.sqlconn;

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



public class Util {
    Connection conn=null;
    
    
    public Util(){
        
    // 第一步    
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            
            System.out.println("加载驱动失败");
        }
        
    //第二步
        String url = "jdbc:mysql://10.1.5.205:3306/admin ";
        String user = "root ";
        String password = "admin ";
        try {
             conn =  DriverManager.getConnection(url, user, password) ;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("数据库连接失败");
        }
        
        
        //第三步  操作数据库
        try {
            Statement sta = conn.createStatement();
            String sql  = "select * from userinfor ; " ;
            
            ResultSet rs = sta.executeQuery(sql);
            
            while(rs.next()){
                String name= rs.getString("name");
                String pwd= rs.getString("pwd");
                System.out.println("name: "+name + "\n pwd: " +pwd);
                
            }
            
            //rs.close();
            //sta.close();
            //conn.close();
                
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("数据库连接失败");
        }
        
        
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
 new Util();
    }

}

bubuko.com,布布扣

 

bubuko.com,布布扣

2014=9=24 连接数据库2

标签:style   blog   http   color   io   os   java   ar   for   

原文地址:http://www.cnblogs.com/fantasy12436109/p/3991680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!