加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Java连接MySQL数据库之mysql-connector-java

(2013-07-17 17:31:49)
标签:

java

数据库

分类: 开发环境安装配置

首先,到官网下载mysql-connector-java

http://dev.mysql.com/downloads/mirror.php?id=412737

   http://s12/bmiddle/8af10696ge1b1cb3d8feb&690

-》(点最下方的No thanks)下载mysql-connector-java-5.1.25.zip

==========================================================================================

安装:

附安装使用教程:http://database.51cto.com/art/201006/204217.htm
-》解压缩到文件夹

   http://s1/mw690/8af10696ge1b1d86ce8b0&690
-》在Eclipse中用到数据库连接的工程名右键-》build path-》configure build path-》Libraries-》add external jars 选择上述加压文件夹中的jar包mysql-connector-java-5.1.25-bin.jar加载进来。

   http://s15/mw690/8af10696ge1b1e144f05e&690

==========================================================================================

使用代码示例

package DBConnect;

import java.sql.*;

public class DBConnection {
 public static void main(String[] args){
  //驱动程序名//不固定,根据驱动
  String driver = "com.mysql.jdbc.Driver";
  // URL指向要访问的数据库名******
  String url = "jdbc:mysql://localhost/******";
  // MySQL配置时的用户名
  String user = "root";
  // Java连接MySQL配置时的密码******
  String password = "******";
  
  try {
  // 加载驱动程序
  Class.forName(driver);
  
  // 连续数据库
  Connection conn = DriverManager.getConnection(url, user, password);
  if(!conn.isClosed())
   System.out.println("Succeeded connecting to the Database!");
  
  // statement用来执行SQL语句
  Statement statement = conn.createStatement();

  // 要执行的SQL语句id和content是表review中的项。
  String sql = "select DISTINCT id ,content from review ";
  ResultSet rs = statement.executeQuery(sql);  
  

  //输出id值和content值
  while(rs.next()) {
   System.out.println(rs.getString("id") + "\t" + rs.getString("content")); 
   
  
  rs.close(); 
  conn.close();  
  } catch(ClassNotFoundException e) {  
   System.out.println("Sorry,can`t find the Driver!");  
   e.printStackTrace();  
  } catch(SQLException e) {
   e.printStackTrace();
  } catch(Exception e){
   e.printStackTrace();
  }
  
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有