admin 管理员组

文章数量: 1184232


2024年1月14日发(作者:sqrt的返回值是什么类型)

JAVA(工具是eclipse)连接MySQL所用驱动下载及安装方法

——胡

java连接mysql时,需要安装驱动。如果未安装,会出现找不到“”的错误。

最新版驱动是:mysql-connector-java-5.1.22

下载地址:/share/link?shareid=64178&uk=2585386604

安装驱动程序:

1、下载jdbc的驱动,解压到任一位置中

2、打开eclipse,找到再在windows->preferences->java->installed jres

3、单击Sun JDk….(根据自己的配置选择不一定非得是这一个),然后单击edit

4、点击add external jars,选择压缩包中的

5、点击finish

使用下面的程序测试数据库连接:(密码是你安装数据库时设置的,一下程序中的密码是默认的)

import .*;

public class JDBCTest {

public static void main(String[] args){

// 驱动程序名

String driver = "";

// URL指向要访问的数据库名game

String url = "jdbc:mysql://127.0.0.1:3306/game";

// MySQL配置时的用户名

String user = "root";

// MySQL配置时的密码

String password = "root";

try {

// 加载驱动程序

e(driver);

// 连续数据库

Connection conn = nection(url, user, password);

if(!ed())

n("Succeeded connecting to the Database!");

// statement用来执行SQL语句

Statement statement = Statement();

// 要执行的SQL语句

String sql = "select id,username from user_index order by id desc limit

0,5";

// 结果集

ResultSet rs = eQuery(sql);

n("-----------------");

n("执行结果如下所示:");

n("-----------------");

n(" id" + "t" + " 用户名");

n("-----------------");

String name = null;

while(()) {

// 选择username这列数据

name = ing("username");

// 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。

// 然后使用GB2312字符集解码指定的字节数组

name = new String(es("ISO-8859-1"),"GB2312");

// 输出结果

n(ing("id") + "t" + name);

}

();

();

} catch(ClassNotFoundException e) {

n("Sorry,can`t find the Driver!");

tackTrace();

} catch(SQLException e) {

tackTrace();

} catch(Exception e) {tackTrace();}

}

}


本文标签: 安装 驱动 数据库 连接 密码