package com.sys.store;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.common.*;
public class FSHsysMysqlOperator extends FSHsysStoageBase {
private Connection m_handle = null;
private PreparedStatement statement = null;
public FSHsysMysqlOperator(DBConfig obj) {
m_oDBConfig = obj;
}
public int open( ) {
String url = "jdbc:mysql://" + m_oDBConfig.m_strHostName + ":" + m_oDBConfig.m_strPort + "/"
+ m_oDBConfig.m_strDBName + "?characterEncoding=UTF-8";
System.out.println( url );
try {
Class.forName("com.mysql.jdbc.Driver");
m_handle = DriverManager.getConnection(url, m_oDBConfig.m_strUserName, m_oDBConfig.m_strPasswd);
return 1;
} catch (ClassNotFoundException cnfex) {
cnfex.printStackTrace();
} catch (SQLException sqlex) {
System.out.println(sqlex.getErrorCode() );
sqlex.printStackTrace();
}
return 0;
}
public void close() {
try {
if ( m_handle != null)
m_handle.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public FSHsysResult exec( String sql ){
try {
statement = m_handle.prepareStatement(sql);
FSHsysResult pMysqlResult = new FSHsysMysqlResult();
pMysqlResult.Assign( statement.executeQuery( sql ) );
return pMysqlResult ;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean cmd(String sql){
try {
statement = m_handle.prepareStatement(sql);
statement.executeUpdate( sql );
return true;
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
protected void finalize(){
this.close();
}
}
package com.sys.store;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class FSHsysResult {
private ResultSet m_oResultSet;
public void Assign(ResultSet obj) {
m_oResultSet = obj;
}
public int GetColumnCount() throws SQLException {
try {
return m_oResultSet.getMetaData().getColumnCount();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public int GetRowCount() {
try {
m_oResultSet.last();
int iRow = m_oResultSet.getRow();
m_oResultSet.beforeFirst();
return iRow;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public String GetColumnName(int index) {
try {
return m_oResultSet.getMetaData().getColumnName(index);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String GetValue(int iRow, int iCol ) {
try {
String strType = this.GetType(iCol);
String strValue="";
switch (strType) {
case "INT":
this.cursorMove(iRow);
strValue = Integer.toString(m_oResultSet.getInt(iCol));
break;
case "VARCHAR":
this.cursorMove(iRow);
strValue = m_oResultSet.getString(iCol);
break;
default:
throw new Exception() ;
}
return strValue ;
} catch (Exception e) {
e.printStackTrace();
}
return "" ;
}
private String GetType(int iCol) {
try {
String strType = m_oResultSet.getMetaData().getColumnTypeName(iCol);
return strType;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private void cursorMove(int iRow) {
try {
m_oResultSet.beforeFirst();
int iRowWalk = 0;
while (m_oResultSet.next()) {
iRowWalk++;
if (iRow == iRowWalk) {
return;
}
}
m_oResultSet.beforeFirst();
} catch (Exception e) {
e.printStackTrace();
}
}
}