📄 db.java
字号:
package com.mr.UpLoad;
import java.math.BigDecimal;
import java.sql.*;
/**
* SQL Server2000 DB Manager
* @author suHao
*
*/
public class DB {
private static Connection conn = null;
private static String url =
"jdbc:microsoft:sqlserver://172.16.162.148:1433;DatabaseName=DB_upload14;user=sa;password=as";
static{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
/**
* executeQuery
* @param RO_SQL read Only sql
* @param params array of parameters
* @return ResultSet
*/
public static ResultSet executeQuery(String RO_SQL, Object[] params) {
if (RO_SQL == null || RO_SQL.length() == 0)
throw new IllegalArgumentException("Required parameter missing - " + RO_SQL);
//
ResultSet rs = null;
PreparedStatement pstmt = null;
try
{
pstmt = conn.prepareStatement (RO_SQL, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// Set Parameter
if (params != null)
{
for (int i = 0; i < params.length; i++)
{
Object param = params[i];
if (param == null)
pstmt.setNull(i+1, Types.VARCHAR);
else if (param instanceof String)
pstmt.setString(i+1, (String)param);
else if (param instanceof Integer)
pstmt.setInt(i+1, ((Integer)param).intValue());
else if (param instanceof BigDecimal)
pstmt.setBigDecimal(i+1, (BigDecimal)param);
else if (param instanceof Timestamp)
pstmt.setTimestamp(i+1, (Timestamp)param);
}
}
//
rs = pstmt.executeQuery();
}
catch (SQLException e)
{
System.out.println(RO_SQL + e.getMessage());
// throw new DBException(e);
}
return rs;
} //executeQuery
/**
* Execute Update.
* @param sql
* @return number of rows updated or -1 if error
*/
public static int executeUpdate(String sql) {
return executeUpdate (sql, null);
}
/**
* Execute Update.
* @param sql sql
* @param params array of parameters
* @return number of rows updated or -1 if error
*/
public static int executeUpdate (String sql, Object[] params)
{
if (sql == null || sql.length() == 0)
throw new IllegalArgumentException("Required parameter missing - " + sql);
//
int no = -1;
PreparedStatement pstmt = null;
try
{
pstmt = conn.prepareStatement (sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
// Set Parameter
if (params != null)
{
for (int i = 0; i < params.length; i++)
{
Object param = params[i];
if (param == null)
pstmt.setNull(i+1, Types.VARCHAR);
else if (param instanceof String)
pstmt.setString(i+1, (String)param);
else if (param instanceof Integer)
pstmt.setInt(i+1, ((Integer)param).intValue());
else if (param instanceof BigDecimal)
pstmt.setBigDecimal(i+1, (BigDecimal)param);
else if (param instanceof Timestamp)
pstmt.setTimestamp(i+1, (Timestamp)param);
}
}
//
no = pstmt.executeUpdate();
}
catch (SQLException e)
{
System.out.println(sql + e.getMessage());
// throw new DBException(e);
}
finally
{
// Always close cursor
try
{
pstmt.close();
}
catch (SQLException e2)
{
System.out.println("Cannot close prepareStatement");
}
}
return no;
} // executeUpdate
/**
* DB is connected
* @return
*/
public static boolean isConnected(){
return conn != null;
}
/**
* Connection instance
* @return
*/
public static Connection getConnection()
{
return conn;
}
/**
* Prepare Read Only Statement
* @param RO_SQL sql (RO)
* @param trxName transaction
* @return Prepared Statement
*/
public static PreparedStatement prepareStatement (String RO_SQL)
{
try{
return conn.prepareStatement(RO_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
return null;
}
} // prepareStatement
/**
* Get Value from sql
* @param sql sql
* @param str_param1 parameter 1
* @return first value or -1
*/
public static int getSQLValue (String sql, String str_param1)
{
int retValue = -1;
PreparedStatement pstmt = null;
try
{
pstmt = prepareStatement(sql);
pstmt.setString(1, str_param1);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
retValue = rs.getInt(1);
else
System.out.println("No Value " + sql + " - Param1=" + str_param1);
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
System.out.println( sql + " - Param1=" + str_param1 + e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
return retValue;
} // getSQLValue
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -