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

Mysql 驱动程序,连接数据库的代码

时间:2014-07-06 15:55:17      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:数据   os   cti   for   代码   io   

public static void main(String[] args) {

         String usname= "root";

         String pwd = "000000";  

         String driver="com.mysql.jdbc.Driver";  

         String url = "jdbc:mysql://localhost:3306/zslm?user=root&password=000000"; //zslm是数据库名字,root是用户password//是密码

         String sql = "select * from module";  

         Connection conn = null;  

         Statement stmt = null;  

         ResultSet rs = null;  

         try {  

                           Class.forName(driver);  //加载驱动    

                           conn = DriverManager.getConnection(url); //获得连接  

                           stmt = conn.createStatement();   

                           rs = stmt.executeQuery(sql);   

                           if (rs !=null){    

                                          while(rs.next()){    

                                              System.out.println(rs.getString(1));     

                                              System.out.println(rs.getString(2));    

                                              System.out.println(rs.getDate(3));     

                                              System.out.println(rs.getString(4));     

                                              System.out.println(rs.getString(5));     

                                              System.out.println(rs.getString(6));     

                                             System.out.println(rs.getString(7));     

                                            }            

                                 }else{    

                                 System.out.println("rs是null");   

                                  }  

                                rs.close();   

                               stmt.close();   

                                 rs.close();  

        } catch (ClassNotFoundException e){   

              e.printStackTrace();  

        }catch (SQLException e) {

             e.printStackTrace();  

        }

Mysql 驱动程序,连接数据库的代码,布布扣,bubuko.com

Mysql 驱动程序,连接数据库的代码

标签:数据   os   cti   for   代码   io   

原文地址:http://www.cnblogs.com/xubc/p/3825421.html

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