这一片海,源于那一瓢水

java 连接Mysql(简单示例)

package org.mysqljdbc;

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

public class Jdbc {
 /**
  * @param args
  */
 public static void main(String[] args) {
  ResultSet rs = null;
  Statement stmt = null;
  Connection conn = null;
  String driver = "org.gjt.mm.mysql.Driver";
  String url ="jdbc:mysql://localhost/mydata?user=root&password=root&useUnicode=true&characterEncoding=8859_1";
  
  //加载驱动
  try {
   Class.forName(driver);
   conn = DriverManager.getConnection(url);
   stmt =  (Statement) conn.createStatement();
   rs =stmt.executeQuery("select * from dept");
   while(rs.next())
   {
    System.out.println(rs.getString("deptno"));
    System.out.println(rs.getString("dname"));
   }
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally
  {
   try
   {
    if(rs != null)
    {
     rs.close();
     rs = null;
    }
    if(conn != null)
    {
     conn.close();
     conn = null;
    }
   }catch (Exception e) {
                e.printStackTrace();
   }
  }
 }
}

posted on 2010-06-25 17:09 何方 阅读(124) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿

随笔档案

文章分类

文章档案

相册

搜索

最新评论