mysql4.1.10的JDBC驱动(mysql-connector-java-3.1.7-bin.jar)的配置及测试

时间:2022-09-19 12:32:03

                                                    mysql4.1.10的JDBC驱动的配置及测试
一.下载
mysql4.1.10的下载:
http://dev.mysql.com/get/Downloads/MySQL-4.1/mysql-4.1.10-win32.zip/from/pick
mysql4.1.10jdbc驱动的下载:
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.1.7.zip/from/pick
mysql-front的下载:
MySQL-Front is an easy to use, compact, and very fast graphical tool to manage your MySQL database - from anywhere in the world.
http://www.mysqlfront.de/download.html

二.配置
     1. 把下载的jdbc驱动mysql-connector-java-3.1.7.zip解压缩,提取其中的mysql-connector-java-3.1.7-bin.jar文件到适当位置,并设置classpath路径指向该文件.
     2.安装mysql数据库.
     3.安装mysql-front,利用mysql-front建立数据库jtest,并在jtest中建立表info.然后填入数据.

三.测试
     测试代码如下:
文件名:testMysql.java
/**
 * @author rockaka
 * mysql的jdbc驱动测试代码
 * mysql4.1.10+mysql-connector-java-3.1.7-bin.jar
 * data:2005-02-23
 */

import java.sql.*;

public class testMysql {

 public static void main(String[] args) {
  //驱动程序名
  String driverName="com.mysql.jdbc.Driver";
  //数据库用户名
  String userName="root";
  //密码
  String userPasswd="root";
  //数据库名
  String dbName="jtest";
  //表名
  String tableName="info";
  //联结字符串
  String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
  
  System.out.println(url);
  
  try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();

   Connection connection=DriverManager.getConnection(url);
   Statement statement = connection.createStatement();
   String sql="SELECT * FROM "+tableName;
   ResultSet rs = statement.executeQuery(sql);
//    获得数据结果集合
   ResultSetMetaData rmeta = rs.getMetaData();
//    确定数据集的列数,亦字段数
   int numColumns=rmeta.getColumnCount();
//    输出每一个数据值
   for(int i=1;i<=numColumns;i++) {
       if(i<numColumns)
         System.out.print(rmeta.getColumnName(i)+" | ");
       else
         System.out.println(rmeta.getColumnName(i));
       }
       while(rs.next()){
          for(int i=1;i<=numColumns;i++) {
              if(i<numColumns)
                 System.out.print(rs.getString(i).trim()+" | ");
              else
                 System.out.println(rs.getString(i).trim());
          }
       }
   rs.close();
   connection.close();
   }catch(Exception ex){
      System.out.println(ex);
      System.exit(0);
   }

 }
}

四:编译与运行
      1.编译:javac testMysql.java
      2.运行:java testMysql
      3.我的运行结果:
jdbc:mysql://localhost/jtest?user=root&password=root
NO | NAME | AGE
1 | AAA | 20
2 | BBB | 30
3 | CCC | 40