import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.awt.Toolkit;
import java.sql.Types;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import javax.swing.JOptionPane;
final class GetSQLServerType
{
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
public GetSQLServerType()
{
try{ //实例化MSSQL2000的驱动
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
//连接数据库
connection=DriverManager.getConnection( "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind", "sa", "" );
//创建Statement接口对象
statement=connection.createStatement();
resultSet=statement.executeQuery( " SELECT * FROM SQLServerType " );
metaData=resultSet.getMetaData();
int columnCount=metaData.getColumnCount();
FileWriter fw=new FileWriter( "SQLServerType.htm" );
BufferedWriter bfw=new BufferedWriter( fw );
bfw.write( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">" ); bfw.newLine();
bfw.write( "<title>SQL Server数据类型对应Java类型</title>" ); bfw.newLine();
//申明样式表//////////////////////////////////////////
bfw.write( "<style type=text/css>" ); bfw.newLine();
bfw.write( "td{ color: #000000;" ); bfw.newLine();
bfw.write( " background: #FFFFFF;" ); bfw.newLine();
bfw.write( " height: 20px;" ); bfw.newLine();
bfw.write( " font-size: 10pt;" ); bfw.newLine();
bfw.write( " font-weight: normal;" ); bfw.newLine();
bfw.write( " font-family: 宋体;" ); bfw.newLine();
bfw.write( " text-align: right;" ); bfw.newLine();
bfw.write( " vertical-align: middle;" ); bfw.newLine();
bfw.write( " table-layout: fixed;" ); bfw.newLine();
bfw.write( " padding-top: 2;" ); bfw.newLine();
bfw.write( " padding-bottom: 2;" ); bfw.newLine();
bfw.write( " padding-left: 2;" ); bfw.newLine();
bfw.write( " padding-right: 2;" ); bfw.newLine();
bfw.write( " }" ); bfw.newLine();
bfw.write( "td.left{ text-align: left; }" ); bfw.newLine();
bfw.write( "td.center{ text-align: center; }" ); bfw.newLine();
bfw.write( "</style>" ); bfw.newLine();
bfw.write( "<body>" ); bfw.newLine();
bfw.write( "<table border=1 bordercolor=#000000 cellspacing=0 style=\"border-collapse:collapse\">" );bfw.newLine();
bfw.write( " <tr>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnClassName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnLabel( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getCatalogName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnCount()</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnDisplaySize( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnType( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getColumnTypeName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getPrecision( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getScale( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getSchemaName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.getTableName( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isAutoIncrement( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isCaseSensitive( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isCurrency( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isDefinitelyWritable( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isNullable( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isReadOnly( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isSearchable( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isSigned( column )</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>metaData.isWritable( column )</td>" );bfw.newLine();
bfw.write( " </tr>" );bfw.newLine();
for( int column=0; column<columnCount; column++ )
{
/*/
//metaData的所有方法
bfw.write( "metaData.getColumnCount() " + metaData.getColumnCount() );bfw.newLine();
bfw.write( "metaData.isAutoIncrement( "+( column+1 )+" ) " + metaData.isAutoIncrement( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isCaseSensitive( "+( column+1 )+" ) " + metaData.isCaseSensitive( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isSearchable( "+( column+1 )+" ) " + metaData.isSearchable( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isCurrency( "+( column+1 )+" ) " + metaData.isCurrency( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isNullable( "+( column+1 )+" ) " + metaData.isNullable( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isSigned( "+( column+1 )+" ) " + metaData.isSigned( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnDisplaySize( "+( column+1 )+" ) " + metaData.getColumnDisplaySize( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnLabel( "+( column+1 )+" ) " + metaData.getColumnLabel( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnName( "+( column+1 )+" ) " + metaData.getColumnName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getSchemaName( "+( column+1 )+" ) " + metaData.getSchemaName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getPrecision( "+( column+1 )+" ) " + metaData.getPrecision( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getScale( "+( column+1 )+" ) " + metaData.getScale( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getTableName( "+( column+1 )+" ) " + metaData.getTableName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getCatalogName( "+( column+1 )+" ) " + metaData.getCatalogName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnType( "+( column+1 )+" ) " + metaData.getColumnType( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnTypeName( "+( column+1 )+" ) " + metaData.getColumnTypeName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isReadOnly( "+( column+1 )+" ) " + metaData.isReadOnly( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isWritable( "+( column+1 )+" ) " + metaData.isWritable( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isDefinitelyWritable( "+( column+1 )+" ) " + metaData.isDefinitelyWritable( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.getColumnClassName( "+( column+1 )+" ) " + metaData.getColumnClassName( ( column+1 ) ) );bfw.newLine();
bfw.write( "metaData.isSigned( "+( column+1 )+" ) " + metaData.isSigned( ( column+1 ) ) );bfw.newLine();
bfw.write( "--------------------------------------------------------------------------------" );bfw.newLine();
//*/
bfw.write( " <tr>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnClassName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnLabel( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getCatalogName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnCount() +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnDisplaySize( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnType( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getColumnTypeName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getPrecision( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getScale( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getSchemaName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.getTableName( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isAutoIncrement( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isCaseSensitive( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isCurrency( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isDefinitelyWritable( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isNullable( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isReadOnly( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isSearchable( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isSigned( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " <td noWrap class=center>"+metaData.isWritable( column+1 ) +"</td>" );bfw.newLine();
bfw.write( " </tr>" );bfw.newLine();
}
bfw.write( "</table>" );bfw.newLine();
bfw.write( "</body>" ); bfw.newLine();
bfw.flush();
fw.close();
}
catch( IOException ioe )
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( null, "创建文件失败!", "创建文件错误", JOptionPane.WARNING_MESSAGE );
System.out.println( ioe );
}
catch( ClassNotFoundException cnfe )
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( null, "找不到数据库驱动文件!", "数据库错误", JOptionPane.WARNING_MESSAGE );
System.out.println( cnfe );
}
catch( SQLException se )
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( null, "连接数据库失败!请检查数据库是否运行。", "数据库错误", JOptionPane.WARNING_MESSAGE );
System.out.println( se );
}
finally{ try{ if( resultSet!=null )
{
resultSet.close(); //关闭ResultSet接口实例
}
if( statement!=null )
{
statement.close(); //关闭Statement接口实例
}
if( connection!=null )
{
connection.close(); //关闭Connection接口实例
}
}
catch( SQLException se )
{
System.out.println( se );
}
}
}
public static void main( String[] arg )
{
GetSQLServerType g=new GetSQLServerType();
}
}
posted on 2007-07-03 04:03
NeedJava 阅读(444)
评论(0) 编辑 收藏 所属分类:
Java