免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1116 | 回复: 0
打印 上一主题 下一主题

MySQL Connect-J [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-22 12:18 |只看该作者 |倒序浏览

1.Download mysql-connector-java-3.1.8-bin.jar
From
www.mysql.com
-> Download
2.System Var
CLASSPATH = X:mysql-connector-java-3.1.8-bin.jar
3.Test
3.1 Create Database : TestDB ; Create Table : TestTable
D:Test.sql
# 创建数据库 TestDB
CREATE DATABASE TestDB;
# 选择数据库
USE TestDB;
# 创建表
CREATE TABLE TestTABLE (
    column1 INT(6) NOT NULL,
    column2 CHAR(20),
    column3 CHAR(20),
    column4 CHAR(20)
);
# 添加数据
INSERT INTO TestTABLE VALUES(100001,"A","B","C");
INSERT INTO TestTABLE VALUES(100002,"A","B","C");
INSERT INTO TestTABLE VALUES(100003,"A","B","C");
INSERT INTO TestTABLE VALUES(100004,"A","B","C");
INSERT INTO TestTABLE VALUES(100005,"A","B","C");
向MySQL提交脚本文件 Test.sql
C:Documents and Settingsoger>mysql -u root -p < d:Test.sql
Enter password: ********
3.2 Java Test :
// MySQLViewer
import java.sql.*;
import java.util.*;
class MySQLViewer {
public MySQLViewer() {
  Statement stmt = null;
  ResultSet rs = null;
  String sqlStr = null;
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   //根据当前实际情况修改其中用户名和密码
   Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost/TestDB?user=root&password=88888888");
   stmt = conn.createStatement();
   sqlStr = "SELECT * FROM TestTABLE";
      rs = stmt.executeQuery(sqlStr);
   if (rs != null) {
    Vector columnHeads = new Vector();
    Vector rows = new Vector();   
    //获取字段的名称
    ResultSetMetaData rsmd = rs.getMetaData();
    for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) {     
     columnHeads.addElement( rsmd.getColumnName( i ) );
    }
    //打印字段名称
    for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) {
     // Vector 采用数组方式索引
     System.out.print(columnHeads.get(i-1)+"        ");
    }
    //获取记录集
    // 移动纪录指针到首记录
    rs.beforeFirst();
    while(rs.next()) {
     rows.addElement( getNextRow( rs, rsmd ) );
    }
   }
  }
  catch (Exception e) {
   System.out.println(e.toString());
  }
  finally {
   if (rs != null) {
    try {
     rs.close();
    }
    catch (SQLException sqlEx) {
     rs = null;
    }
   }
   
   if (stmt != null) {
    try {
     stmt.close();
    }
    catch (SQLException sqlEx) {
           stmt = null;
       }
   }
  }   
}
private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd )
       throws SQLException {
  Vector currentRow = new Vector();
        for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) {
            currentRow.addElement( rs.getString( i ) );
     }
     //打印每条记录中每个字段
  System.out.println();
  for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
   System.out.print(currentRow.get(i-1)+"        ");
  
  //返回一条记录   
  return currentRow;
}
public static void main(String[] args) {
  MySQLViewer app = new MySQLViewer();  
}
}
---------- java ----------
column1 column2 column3 column4
100001 A B C
100002 A B C
100003 A B C
100004 A B C
100005 A B C
Output completed (1 sec consumed) - Normal Termination


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3534/showart_32326.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP