java连接mysql数据库例子_JAVA连接MySQL数据库的例子

注意,在执行程序前已经在MySQL中的test数据库中建立了一个表pet,表的结构和内容如下:

代码

D:\>mysql

Welcome to the MySQL monitor.  Commands end with; or \g.

Your MySQL connection id is 70 to server version: 5.0.18-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test

Database changed

mysql> desc pet;

+---------+-------------+------+-----+---------+-------+

| Field   | Type        | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| name    | varchar(20) | YES  |     | NULL    |       |

| owner   | varchar(20) | YES  |     | NULL    |       |

| species | varchar(20) | YES  |     | NULL    |       |

| sex     | char(1)     | YES  |     | NULL    |       |

| birth   | date        | YES  |     | NULL    |       |

| death   | date        | YES  |     | NULL    |       |

+---------+-------------+------+-----+---------+-------+

6 rows in set (0.01 sec)

mysql> select * from pet;

+----------+--------+---------+------+------------+------------+

| name     | owner  | species | sex  | birth      | death      |

+----------+--------+---------+------+------------+------------+

| Fluffy   | Harold | cat     | f    | 1993-02-04 | 0000-00-00 |

| Claws    | Gwen   | cat     | m    | 1994-03-17 | 0000-00-00 |

| Buffy    | Harold | dog     | f    | 1989-05-13 | 0000-00-00 |

| Fang     | Benny  | dog     | m    | 1990-08-27 | 0000-00-00 |

| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |

| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | 0000-00-00 |

| Whistler | Gwen   | bird    | NULL | 1997-12-09 | 0000-00-00 |

| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |

| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |

+----------+--------+---------+------+------------+------------+

9 rows in set (0.00 sec)

mysql>

JAVA程序的内容如下

代码

import java.sql.*;

public class SampleIntro

{

public static void main(String[] args)

{

try

{

Connection conn;

Statement stmt;

ResultSet res;

//加载Connector/J驱动

//这一句也可写为:Class.forName("com.mysql.jdbc.Driver");

Class.forName("com.mysql.jdbc.Driver").newInstance();

//建立到MySQL的连接

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",

"root", "guapo");

//执行SQL语句

stmt = conn.createStatement();

res = stmt.executeQuery("select * from pet");

//处理结果集

while (res.next())

{

String name = res.getString("name");

System.out.println(name);

}

res.close();

}

catch (Exception ex)

{

System.out.println("Error : " + ex.toString());

}

}

}

下面是执行结果

代码

D:\>javac SampleIntro.java

D:\>java SampleIntro

Fluffy

Claws

Buffy

Fang

Bowser

Chirpy

Whistler

Slim

Puffball

D:\>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供 Java 连接 MySQL 数据库的示例代码。以下是一个简单的例子: ``` import java.sql.*; public class MySQLDemo { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 加载 MySQL 驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 连接数据库 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "password"; conn = DriverManager.getConnection(url, user, password); // 创建一个 Statement 对象 stmt = conn.createStatement(); // 执行查询 rs = stmt.executeQuery("SELECT * FROM employees"); // 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); System.out.println("Employee ID: " + id + ", Name: " + name + ", Email: " + email); } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在这个例子中,我们首先加载了 MySQL 驱动程序,并使用 `DriverManager.getConnection` 方法连接MySQL 数据库。然后,我们创建了一个 `Statement` 对象,并使用 `executeQuery` 方法执行了一个查询语句。最后,我们遍历了结果集并输出了查询结果。 希望这个例子能够帮助您开始使用 Java 连接 MySQL 数据库。如果您还有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值