java测试数据库连接

 时间:2026-02-12 00:48:37

1、JDBC是一种底层API,在访问数据库是需要在业务逻辑中直接嵌入SQL语句。JDBC不能够直接访问数据库,必须依赖数据库厂商提供的JDBC驱动程序来实现Java程序与MySql的交互。

http://dev.mysql.com/downloads/connector/

点击“Connector/J”链接

java测试数据库连接

2、在打开的页面,点

Platform Independent (Architecture Independent), ZIP Archive 

(mysql-connector-java-5.1.38.zip)

后面的“download”链接

Tips:

当然下载

“Platform Independent (Architecture Independent), Compressed TAR Archiv”(mysql-connector-java-5.1.38.tar.gz)

也可以

java测试数据库连接

3、在打开的页面,点击下面的“No thanks, just start my download.”链接

http://120.52.72.59/cdn.mysql.com/c3pr90ntcsf0//Downloads/Connector-J/mysql-connector-java-5.1.38.zip

Tips:

当然,注册一个帐户,然后再下载也可以 

java测试数据库连接

java测试数据库连接

4、解压缩下载的mysql-connector-java-5.1.38.zip

找到我们需要的mysql-connector-java-5.1.38-bin.jar

java测试数据库连接

5、把mysql-connector-java-5.1.38-bin.jar添加到Project的依赖中

java测试数据库连接

6、万事俱备,只剩下写代码

Code:

package jdbc;

import java.sql.*;

/**

* Created by MyWorld on 2016/4/4.

*/

public class JDBCForMysql {

   public static void main(String[] args) {

       Connection conn = null;

       Statement statement = null;

       ResultSet resultSet = null;

       try {

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

           String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8";

           conn = DriverManager.getConnection(url, "mysql", "mysql");

           System.out.println("Connection status, isClosed: " + conn.isClosed());            statement = conn.createStatement();

           statement.executeUpdate("CREATE TABLE IF NOT EXISTS staff (id varchar(20),name varchar(20),lastModifiedTime datetime)");

           for (int i = 0; i < 10; i++) {

               String sql = String.format("insert into staff (id,name,lastModifiedTime) values('%s','%s',current_timestamp()) ", "no" + i, "name" + i);

               statement.addBatch(sql);

           }

           statement.executeBatch();

           resultSet = statement.executeQuery("select id,name,lastModifiedTime from staff");

           ResultSetMetaData metaData = resultSet.getMetaData();

           int columnCount = metaData.getColumnCount();

           for (int i = 1; i <= columnCount; i++) {

               System.out.print(metaData.getColumnLabel(i) + "\t");

           }

           System.out.println("");

           while (resultSet.next()) {

               for (int i = 1; i <= columnCount; i++) {

                   System.out.print(resultSet.getString(i) + "\t");

               }

               System.out.println("");

           }

       } catch (ClassNotFoundException e) {

           e.printStackTrace();

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           if (resultSet != null) {

               try {

                   resultSet.close();

               } catch (SQLException ignored) {

               }

           }            if (statement != null) {

               try {

                   statement.close();

               } catch (SQLException ignored) {

               }

           }

           if (conn != null) {

               try {

                   conn.close();

               } catch (SQLException ignored) {

               }

           }

       }

   }

}

java测试数据库连接

7、执行下看看结果:

Output:

Connection status, isClosed: false

id name lastModifiedTime

no0 name0 2016-04-04 16:47:02.0

no1 name1 2016-04-04 16:47:03.0

no2 name2 2016-04-04 16:47:03.0

no3 name3 2016-04-04 16:47:03.0

no4 name4 2016-04-04 16:47:03.0

no5 name5 2016-04-04 16:47:03.0

no6 name6 2016-04-04 16:47:04.0

no7 name7 2016-04-04 16:47:04.0

no8 name8 2016-04-04 16:47:04.0

no9 name9 2016-04-04 16:47:04.0

java测试数据库连接

  • 怎么找到C盘里users文件夹里的AppData文件夹
  • Edge浏览器手机版启动就出现自己想要的主页
  • QListWidget怎样实现全选
  • 如何安装APlayer播放引擎组件?
  • Firefox浏览器如何查看安全设备
  • 热门搜索
    合肥旅游 嵊泗列岛旅游攻略 旅游景区 黄山旅游 昆明旅游攻略 马尔代夫旅游攻略 新疆旅游 乌镇旅游 南澳岛旅游攻略 中国旅游景点