JDBC连接数据库

jdbc连接数据库:

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcConnect {
    private static final String DBDRIVER = "com.mysql.jdbc.Driver";  
    private static final String DBURL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";  
    private static final String DBUSER = "root";  
    private static final String DBPASS = "root"; 

    /**
     * @param args
     */
    public static void main(String[] args) {
        /**
         * jdbc操作数据库  
         */
        try {  
            Class.forName(DBDRIVER);  
            Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);  
            
            String sql = "select * from user";
            PreparedStatement psmt = conn.prepareStatement(sql);  
            ResultSet set = psmt.executeQuery();
            while(set.next()){
                String subbranch = set.getString("userName");
                System.out.println(subbranch);
            }
        } catch (Exception e) {  
            e.printStackTrace();  
        }  

    }

}

 

posted @ 2017-11-01 10:27  super超人  阅读(201)  评论(0编辑  收藏  举报