mysql jdbc驱动_安装 mysql 的 JDBC driver

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 JavaCLASSPATH.

如何安装呢?

有很多办法。

最简单的办法:

把下载 的文件解压后的  。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 classMysqlJdbc {public static voidmain(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

packageDB;import java.sql.*;classJdbc

{public static void main(String[] args)throwsException

{//只有下面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 classJdbc {public static voidmain(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/6701267http://lavasoft.blog.51cto.com/62575/20588

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值