📄 msqlutil.java
字号:
package net.jumperz.sql;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.math.*;
import java.util.*;
import net.jumperz.util.*;
public class MSqlUtil
{
// --------------------------------------------------------------------------------
public static void closeConnection( Connection connection )
{
try
{
if( connection != null && !connection.isClosed() )
{
connection.close();
}
}
catch( SQLException e )
{
}
}
//--------------------------------------------------------------------------------
public static String getJdbcDriverClassName( String dbmsUrl )
{
if( dbmsUrl.indexOf( "jdbc:hsqldb:" ) == 0 )
{
return "net.jumperz.ext.org.hsqldb.jdbcDriver";
}
else if( dbmsUrl.indexOf( "jdbc:postgresql" ) == 0 )
{
return "org.postgresql.Driver";
}
else if( dbmsUrl.indexOf( "jdbc:firebirdsql" ) == 0 )
{
return "org.firebirdsql.jdbc.FBDriver";
}
else if( dbmsUrl.indexOf( "jdbc:mysql" ) == 0 )
{
return "com.mysql.jdbc.Driver";
}
else if( dbmsUrl.indexOf( "jdbc:interbase" ) == 0 )
{
return "interbase.interclient.Driver";
}
else if( dbmsUrl.indexOf( "jdbc:sybase" ) == 0 )
{
return "com.sybase.jdbc.SybDriver";
}
else if( dbmsUrl.indexOf( "jdbc:oracle" ) == 0 )
{
return "oracle.jdbc.driver.OracleDriver";
}
else if( dbmsUrl.indexOf( "jdbc:sqlserver" ) == 0 )
{
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
else if( dbmsUrl.indexOf( "jdbc:db2" ) == 0 )
{
return "com.ibm.db2.jcc.DB2Driver";
//return "COM.ibm.db2.jdbc.app.DB2Driver";
}
return null;
}
//--------------------------------------------------------------------------------
public static String[] resultSetToStrArray( ResultSet rs, String charsetName )
throws SQLException
{
ResultSetMetaData md = rs.getMetaData();
int count = md.getColumnCount();
rs.next();
String[] strArray = new String[ count ];
for( int i = 0; i < count; ++i )
{
String tmpStr = rs.getString( i + 1 );
if( tmpStr == null )
{
strArray[ i ] = "";
}
else
{
strArray[ i ] = decode( tmpStr , charsetName );
}
}
return strArray;
}
//--------------------------------------------------------------------------------
public static List resultSetToList( ResultSet rs, String charsetName )
throws SQLException
{
ResultSetMetaData md = rs.getMetaData();
List list = new ArrayList();
int count = md.getColumnCount();
while( rs.next() )
{
String[] strArray = new String[ count ];
for( int i = 0; i < count; ++i )
{
String tmpStr = rs.getString( i + 1 );
if( tmpStr == null )
{
strArray[ i ] = "";
}
else
{
strArray[ i ] = decode( tmpStr , charsetName );
}
}
list.add( strArray );
}
return list;
}
//--------------------------------------------------------------------------------
private static void setArgs( PreparedStatement ps, MObjectArray args )
throws SQLException
{
int index = 1;
List list = args.toList();
for( int i = 0; i < list.size(); ++i, ++index )
{
Object arg = list.get( i );
if( arg instanceof String )
{
ps.setString( index, ( String )arg );
}
else if( arg instanceof Integer )
{
ps.setInt( index, ( ( Integer )arg ).intValue() );
}
else if( arg instanceof Long )
{
ps.setLong( index, ( ( Long )arg ).longValue() );
}
else if( arg instanceof Boolean )
{
ps.setBoolean( index, ( ( Boolean )arg ).booleanValue() );
}
else if( arg instanceof BigDecimal )
{
ps.setBigDecimal( index, ( BigDecimal )arg );
}
else if( arg instanceof Timestamp )
{
ps.setTimestamp( index, ( Timestamp )arg );
}
}
}
//--------------------------------------------------------------------------------
public static int getInt2( Connection connection, String queryString, MObjectArray args )
throws SQLException
{
PreparedStatement ps = connection.prepareStatement( queryString );
setArgs( ps, args );
ResultSet rs = ps.executeQuery();
rs.next();
return rs.getInt( 1 );
}
//--------------------------------------------------------------------------------
public static int executeUpdate2( Connection connection, String queryString, MObjectArray args )
throws SQLException
{
if( queryString.indexOf( "???" ) >= 0 )
{
queryString = queryString.replaceFirst( "\\?\\?\\?", args.toQues() );
}
PreparedStatement ps = connection.prepareStatement( queryString );
setArgs( ps, args );
return ps.executeUpdate();
}
// --------------------------------------------------------------------------------
public static void executeUpdate2( Connection connection, String queryString, Object o )
throws SQLException
{
executeUpdate2( connection, queryString, new MObjectArray( o ) );
}
//--------------------------------------------------------------------------------
public static ResultSet executeQuery2( Connection connection, String queryString, MObjectArray args )
throws SQLException
{
PreparedStatement ps = connection.prepareStatement( queryString );
setArgs( ps, args );
ResultSet resultSet = ps.executeQuery();
return resultSet;
}
//-------------------------------------------------------------------------------
public static void executeUpdate( Connection connection, String queryString )
throws SQLException
{
executeUpdate( connection, queryString, null );
}
//-------------------------------------------------------------------------------
public static void executeUpdate( Connection connection, String queryString, String[] args )
throws SQLException
{
PreparedStatement pStatement = connection.prepareStatement( queryString );
if( args != null )
{
for( int i = 0; i < args.length; ++i )
{
pStatement.setString( i + 1, args[ i ] );
}
}
pStatement.executeUpdate();
}
//-------------------------------------------------------------------------------
public static ResultSet executeQuery( Connection connection, String queryString )
throws SQLException
{
return MSqlUtil.executeQuery( connection, queryString, null );
}
//-------------------------------------------------------------------------------
public static ResultSet executeIntQuery( Connection connection, String queryString, int[] args )
throws SQLException
{
PreparedStatement pStatement = connection.prepareStatement( queryString );
if( args != null )
{
for( int i = 0; i < args.length; ++i )
{
pStatement.setInt( i + 1, args[ i ] );
}
}
return pStatement.executeQuery();
}
//-------------------------------------------------------------------------------
public static ResultSet executeQuery( Connection connection, String queryString, String[] args )
throws SQLException
{
PreparedStatement pStatement = connection.prepareStatement( queryString );
if( args != null )
{
for( int i = 0; i < args.length; ++i )
{
pStatement.setString( i + 1, args[ i ] );
}
}
return pStatement.executeQuery();
}
//-------------------------------------------------------------------------------
public static int getInt( Connection connection, String queryString, String[] args )
throws SQLException
{
PreparedStatement pStatement = connection.prepareStatement( queryString );
if( args != null )
{
for( int i = 0; i < args.length; ++i )
{
pStatement.setString( i + 1, args[ i ] );
}
}
ResultSet resultSet = pStatement.executeQuery();
resultSet.next();
return resultSet.getInt( 1 );
}
//-------------------------------------------------------------------------------
public static int getInt( Connection connection, String queryString )
throws SQLException
{
return getInt( connection, queryString, null );
}
//--------------------------------------------------------------------------------
public static String getString( Connection connection, String queryString )
throws SQLException
{
return MSqlUtil.getString( connection, queryString, null );
}
//--------------------------------------------------------------------------------
public static String getString( Connection connection, String queryString, String[] args )
throws SQLException
{
PreparedStatement pStatement = connection.prepareStatement( queryString );
if( args != null )
{
for( int i = 0; i < args.length; ++i )
{
pStatement.setString( i + 1, args[ i ] );
}
}
ResultSet resultSet = pStatement.executeQuery();
resultSet.next();
return resultSet.getString( 1 );
}
//--------------------------------------------------------------------------------
public static String decode( String in, String CHARSET )
{
try
{
String tmpStr = java.net.URLDecoder.decode( in, CHARSET );
byte[] byteArray = tmpStr.getBytes( CHARSET );
return new String( byteArray, "ISO-8859-1" );
}
catch( UnsupportedEncodingException e )
{
e.printStackTrace();
}
return null;
}
//----------------------------------------------------------------------------------------
public static String encode( String in, String CHARSET )
{
try
{
byte[] byteArray = in.getBytes( "ISO-8859-1" );
String tmpStr = new String( byteArray, CHARSET );
return java.net.URLEncoder.encode( tmpStr, CHARSET );
}
catch( UnsupportedEncodingException e )
{
e.printStackTrace();
}
return null;
}
//----------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -