📄 databaseopen.java
字号:
} else if (key[i] instanceof Double) {
query += safeDoubleToString((Double)key[i]);
} else {
query += key[i].toString();
}
} else {
query += "NULL";
}
}
for (int i = 0; i < result.length; i++) {
query += ',';
if (result[i] != null) {
if (result[i] instanceof String) {
query += '"' + result[i].toString() + '"';
} else if (result[i] instanceof Double) {
query += safeDoubleToString((Double)result[i]);
} else {
query += result[i].toString();
}
} else {
query += "NULL";
}
}
query += ')';
if (Debug) {
System.err.println("Submitting result: " + query);
}
if (stmt.execute(query)) {
if (Debug) {
System.err.println("...acceptResult returned resultset");
}
}
}
/**
* Returns the name associated with a SQL type.
*
* @param type the SQL type
* @return the name of the type
*/
static public String typeName(int type) {
switch (type) {
case Types.BIGINT :
return "BIGINT ";
case Types.BINARY:
return "BINARY";
case Types.BIT:
return "BIT";
case Types.CHAR:
return "CHAR";
case Types.DATE:
return "DATE";
case Types.DECIMAL:
return "DECIMAL";
case Types.DOUBLE:
return "DOUBLE";
case Types.FLOAT:
return "FLOAT";
case Types.INTEGER:
return "INTEGER";
case Types.LONGVARBINARY:
return "LONGVARBINARY";
case Types.LONGVARCHAR:
return "LONGVARCHAR";
case Types.NULL:
return "NULL";
case Types.NUMERIC:
return "NUMERIC";
case Types.OTHER:
return "OTHER";
case Types.REAL:
return "REAL";
case Types.SMALLINT:
return "SMALLINT";
case Types.TIME:
return "TIME";
case Types.TIMESTAMP:
return "TIMESTAMP";
case Types.TINYINT:
return "TINYINT";
case Types.VARBINARY:
return "VARBINARY";
case Types.VARCHAR:
return "VARCHAR";
default:
return "Unknown";
}
}
public Statement getSQLStatement() {
return stmt;
}
//Execute an SQL query
public boolean execute (String query) throws Exception{
return stmt.execute(query);
}
// Return Connection
public Connection getConnection () {
return con;
}
public void setURL ( String aUrl ) {
url = aUrl;
}
public void setUsername ( String aUsername ) {
user = aUsername;
}
public void setPassword ( String aPassword ) {
pass = aPassword;
}
//Check if a table exists
public boolean tableExists(String tableName) throws Exception {
if (Debug) {
System.err.println("Checking if table " + tableName + " exists...");
}
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getTables (null, null, tableName, null);
boolean tableExists = rs.next();
if (rs.next()) {
throw new Exception("This table seems to exist more than once!");
}
rs.close();
if (Debug) {
if (tableExists) {
System.err.println("... " + tableName + " exists");
} else {
System.err.println("... " + tableName + " does not exist");
}
}
return tableExists;
}
public boolean containsKey(String key, String fieldName, String tableName){
try {
ResultSet rset = stmt.executeQuery("SELECT " + fieldName +
" FROM " + tableName +
" WHERE " + fieldName +
" = '" + key + "'" );
String data;
while ( rset.next() ){
data = rset.getString(fieldName);
if ( data.equalsIgnoreCase(key)) {
return true;
}
}
}
catch ( SQLException e ) {
log.error("*** DatabaseOpen.containsKey(String, String, String): SQLException> "+e);
}
catch ( NullPointerException e ) {
log.error("*** DatabaseOpen.containsKey(String, String, String): NullPointerException> "+e);
}
return false;
}
/**
* Returns true if a certain key found in a certain field,
* where an other key from an other field has a particular value
* @param key The key to find
* @param keyFieldName The field of the searched key
* @param whereKey The other key
* @param whereKeyFieldName The field of the other key
* @param tableName The name of the table in database that contains all the above
*/
public boolean containsKey(String key, String keyFieldName, String whereKey, String whereKeyFieldName, String tableName){
try {
ResultSet rset = stmt.executeQuery("SELECT " + keyFieldName +
" FROM " + tableName +
" WHERE " + whereKeyFieldName +
" = '" + whereKey + "'" );
String data;
while ( rset.next() ){
data = rset.getString(keyFieldName);
if ( data.equalsIgnoreCase(key)) {
return true;
}
}
}
catch ( SQLException e ) {
log.error("*** DatabaseOpen.containsKey(String, String, String, String, String): SQLException> "+e);
}
return false;
}
public boolean containsKey(int key, String fieldName, String tableName){
try {
ResultSet rset = stmt.executeQuery("SELECT " + fieldName +
" FROM " + tableName +
"WHERE " + fieldName +
" = " + key );
int data;
while ( rset.next() ){
data = rset.getInt(fieldName);
if ( data == key ) {
return true;
}
}
}
catch ( SQLException e ) {
log.error("*** Database.containsKey(int, String, String): SQLException> "+e);
}
return false;
}
/* This method knows the types of data to be inserted */
public void insertKeysIntoTable(String[] values, String tableName){
String SQLStatement = "INSERT INTO "+tableName+" VALUES (";
for ( int i = 0 ; i < values.length; i++ ) {
SQLStatement += "'";
SQLStatement += values[i];
SQLStatement += "'";
if ( i != values.length - 1 ) {
SQLStatement += ", ";
}
}
SQLStatement += ") ";
try {
stmt.executeUpdate( SQLStatement );
}
catch (SQLException e ) {
log.error("*** Database.insertKeysIntoTable(): SQLException> "+e);
}
catch (NullPointerException e ) {
log.error("*** Database.insertKeysIntoTable(): NullPointerException> "+e);
}
}
private String safeDoubleToString(Double number) {
String orig = number.toString();
int pos = orig.indexOf('E');
if ((pos == -1) || (orig.charAt(pos + 1) == '-')) {
return orig;
} else {
StringBuffer buff = new StringBuffer(orig);
buff.insert(pos + 1, '+');
return new String(buff);
}
}
/*
public static void main ( String[] args ) {
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -