📄 psmethod.java
字号:
package com.ywh.dbcp;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class PSMethod{
private String _tableName;
public PSMethod(String tableName){
_tableName = tableName;
}
public String getSQLList(String [] fields){
StringBuffer result = new StringBuffer();
for (int i = 0; i < fields.length -1; i++) {
result.append( fields[i] );
result.append( " , " );
}
result.append( fields[fields.length-1] );
return result.toString();
}
public boolean insert (Connection conn,
String [] fieldnames,
Object [] fieldvalues) throws SQLException {
// Generate an array of question marks for the SQL template parameters
String [] paras = new String[fieldnames.length];
for (int i = 0; i < fieldnames.length; i++) {
paras[i] = "?";
}
// Prepare the template
String SQLstr = "INSERT INTO " + _tableName + " ( " +
getSQLList(fieldnames) + " ) VALUES ( " +
getSQLList(paras) + " ) ";
PreparedStatement pstmt = conn.prepareStatement(SQLstr);
// set parameter values
for (int i = 0; i < fieldnames.length; i++) {
pstmt.setObject(i + 1, fieldvalues[i]);
}
// execute SQL statement
boolean succ = pstmt.execute();
pstmt.close();
return succ;
}
public Object [] select (Connection conn,
String [] fieldnames,
String idFieldname,
Object idValue) throws SQLException {
Object [] result = new Object [fieldnames.length];
// Prepared the template
String SQLstr = "SELECT " + getSQLList(fieldnames) + " FROM " +
_tableName + " WHERE " + idFieldname + " = ?";
PreparedStatement pstmt = conn.prepareStatement(SQLstr);
// set parameter value
pstmt.setObject(1, idValue);
// execute SQL statement
ResultSet rs = pstmt.executeQuery();
// go to the first record
rs.next();
for ( int i = 0; i < fieldnames.length; i++) {
result[i] = rs.getObject(i+1);
}
rs.close();
pstmt.close();
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -