安装 mysql 的 JDBC driver

去mysql网站http://dev.mysql.com/downloads/connector/ 下载 JDBC driver

http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing.html

You can install the Connector/J package using either the binary or source distribution. The binary distribution provides the easiest method for installation; the source distribution lets you customize your installation further. With either solution, you manually add the Connector/J location to your Java CLASSPATH.

如何安装呢?

有很多办法。

最简单的办法:

把下载 的文件解压后的  。jar文件 copy到 %JAVA_HOME%\jre\lib\ext 下, %JAVA_HOME就是jdk的安装目录。

这样做之后,每次新建的project 查看 system library 时都会看到 mysql-connector-java-5.1.22-bin.jar这个名字。这正是我们想要的。

 

还可以在单个项目导入这个jar,选择所选的项目,右键点击my--->build Path--->add external Archiver...选择jdbc驱动,确定。这样做很麻烦,下次建造工程又需要jdbc又要导入。

 

还可以选择window->preference->java->bulid path-->classpath 增加jar包。这样做也是一劳永逸。

 

下面是测试的代码:

复制代码
import java.sql.*;
public class MysqlJdbc {
  public static void main(String args[]) {
    try {
      Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序   
      //Class.forName("org.gjt.mm.mysql.Driver");
     System.out.println("Success loading Mysql Driver!");
    }
    catch (Exception e) {
      System.out.print("Error loading Mysql Driver!");
      e.printStackTrace();
    }
    try {
      Connection connect = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test","root","198876");
           //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码

      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from user");
                                                              //user 为你表的名称
      while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}
复制代码

注意导入的包是

import java.sql.*; 导入其他的包会错误。
运行结果:

Success loading Mysql Driver!
Success connect Mysql server!

xxx 数据

JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC为工具/数据库开发人员提供了一个标准的API,据此可以构建更高级的工具和接口,使数据库开发人员能够用纯 Java API 编写数据库应用程序,

使用JDBC的步骤分为6步

使用JDBC的步骤1. load the driver

(1)Class.forName()|Class.forName().newlnstance()|new DriverName()

(2)实例化时自动向DriverManager注册,不需要显示调用DriverManager.registerDriver

使用JDBC的步骤2. Connect to the DataBase

DriverManager.getConnection()

使用JDBC的步骤3.Excute the SQL

(1)connection.CreateStatement() 

(2)Statement.excuteQuery()

(3)Statement.executeUpdate()

使用JDBC的步骤4. Retrieve the result data

循环取得结果 while(rs.next())

使用JDBC的步骤5. show the result data

将数据库中的各种类型转换为java中的类型(getXXX)方法

使用JDBC的步骤6. close

close the resultset / close the  statement /close the connection

复制代码
package DB;     
import java.sql.*;     
class  Jdbc     
{     
    public static void main(String[] args)throws Exception     
    {          
        //只有下面2句话就可以连接到数据库中     
        Class.forName("com.mysql.jdbc.Driver");        
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");    //Class.forName("com.mysql.jdbc.Driver");        
        //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");     
 //Class.forName("oracal.jdbc.driver.OracalDriver");     
        //new oracal.jdbc.driver.OracalDriver();     
        //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"     
             
        //jdbc.driverClassName=com.mysql.jdbc.Driver;     
        //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;     
    }     
}    

复制代码

 

链接和操作数据库的时候有2个异常要捕获,如下:

复制代码
import java.sql.*;
public class Jdbc {
    public static void main(String[] args) {     
            
      try {
                Class.forName("com.mysql.jdbc.Driver");     
   
                Connection conn = DriverManager.getConnection(     
                        "jdbc:mysql://localhost:3306/test", "root", "1234");     
   
                Statement stmt = conn.createStatement();     
                ResultSet rs = stmt.executeQuery("select * from test.admin");     
   
                while (rs.next()) {     
                    System.out.println(rs.getString("username"));     
                    System.out.println(rs.getInt("id"));     
                }       
   
                 rs.close();     
              stmt.close();     
               conn.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }     
       
  }
}
复制代码
我们可以先写完代码,写完后全选所写的代码,点击右键选择Surround by try catch ,编译器会自动帮我们把异常语句写好。
注意catch ,我们只要一个try,可以有2个catch语句。


http://blog.csdn.net/zhazha1980518/article/details/6701267
http://lavasoft.blog.51cto.com/62575/20588

 转自:http://www.cnblogs.com/youxin/archive/2012/09/30/2709286.html

MySQL JDBC Driver是Java中用于连接MySQL数据库的JDBC驱动程序。它是一个独立的库,可以通过下载和安装来使用。 要使用MySQL JDBC Driver,您需要执行以下步骤: 1. 下载MySQL JDBC Driver库:您可以从MySQL官方网站(https://dev.mysql.com/downloads/connector/j/)下载MySQL JDBC Driver。 2. 将库添加到您的Java项目:将下载的库文件添加到您的Java项目中的类路径中。 3. 建立数据库连接:使用JDBC API中的DriverManager.getConnection()方法建立与MySQL数据库的连接。 以下是使用MySQL JDBC Driver建立数据库连接的示例代码: ``` import java.sql.*; public class MySQLExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "username"; String password = "password"; try { Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Database connection successful!"); } catch (SQLException e) { System.out.println("Database connection failed!"); e.printStackTrace(); } } } ``` 在这个示例中,我们使用了DriverManager.getConnection()方法来建立与MySQL数据库的连接。这个方法需要传递三个参数:URL、用户名和密码。URL是连接字符串,指定要连接的MySQL服务器和数据库。用户名和密码是用于访问MySQL数据库的凭据。如果连接成功,我们将打印一条成功消息。如果连接失败,我们将打印一条失败消息并打印异常堆栈跟踪。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值