本文整理匯總了Java中com.mysql.jdbc.PreparedStatement類的典型用法代碼示例。如果您正苦於以下問題:Java PreparedStatement類的具體用法?Java PreparedStatement怎麽用?Java PreparedStatement使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PreparedStatement類屬於com.mysql.jdbc包,在下文中一共展示了PreparedStatement類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testIsJdbcInterface
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
/**
* Tests Util.isJdbcInterface()
*
* @throws Exception
*/
public void testIsJdbcInterface() throws Exception {
// Classes directly or indirectly implementing JDBC interfaces.
assertTrue(Util.isJdbcInterface(PreparedStatement.class));
assertTrue(Util.isJdbcInterface(StatementImpl.class));
assertTrue(Util.isJdbcInterface(Statement.class));
assertTrue(Util.isJdbcInterface(ResultSetImpl.class));
Statement s = (Statement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { Statement.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
});
assertTrue(Util.isJdbcInterface(s.getClass()));
// Classes not implementing JDBC interfaces.
assertFalse(Util.isJdbcInterface(Util.class));
assertFalse(Util.isJdbcInterface(UtilsTest.class));
}
示例2: getVehiclebyIdAndVersion_WithNoMatches_ReturnsNull
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test
public void getVehiclebyIdAndVersion_WithNoMatches_ReturnsNull() throws SQLException {
final int vehicleId = 890321;
final int version = 23000;
PreparedStatement mockStatement2 = mock(PreparedStatement.class);
when(mockStatement2.executeQuery()).thenReturn(mockResultSet);
when(mockConnection.prepareStatement(VehicleReadSql.queryGetVehicleByIdAndVersion))
.thenReturn(mockStatement);
when(mockConnection.prepareStatement(VehicleReadSql.queryGetVehicleHistByIdAndVersion))
.thenReturn(mockStatement2);
Vehicle actual = vehicleReadReadDao.getVehicleByIdAndVersion(vehicleId, version);
verify(mockStatement).setObject(1, vehicleId);
verify(mockStatement).setObject(2, version);
verify(mockStatement2).setObject(1, vehicleId);
verify(mockStatement2).setObject(2, version);
assertThat(actual, nullValue());
}
示例3: getVehiclebyIdAndVersion_WithMatches_ReturnsVehicle
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test
public void getVehiclebyIdAndVersion_WithMatches_ReturnsVehicle() throws SQLException {
final int vehicleId = 890321;
final int version = 23000;
PreparedStatement mockStatement2 = mock(PreparedStatement.class);
ResultSet resultSet2 = mock(ResultSet.class);
ResultSetMockHelper.initialize(resultSet2);
when(resultSet2.next()).thenReturn(true);
when(mockStatement2.executeQuery()).thenReturn(resultSet2);
when(mockConnection.prepareStatement(VehicleReadSql.queryGetVehicleByIdAndVersion))
.thenReturn(mockStatement2);
when(mockConnection.prepareStatement(argThat(not(equalTo(VehicleReadSql.queryGetVehicleByIdAndVersion)))))
.thenReturn(mockStatement);
Vehicle actual = vehicleReadReadDao.getVehicleByIdAndVersion(vehicleId, version);
verify(mockStatement2).setObject(1, vehicleId);
verify(mockStatement2).setObject(2, version);
assertThat(actual, notNullValue());
}
示例4: getVehiclebyIdAndVersion_ExecuteThrows_ThrowsInternalException
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test(expected = InternalException.class)
public void getVehiclebyIdAndVersion_ExecuteThrows_ThrowsInternalException() throws SQLException, InternalException {
final int vehicleId = 890321;
final int version = 23000;
PreparedStatement mockStatement2 = mock(PreparedStatement.class);
ResultSet resultSet2 = mock(ResultSet.class);
ResultSetMockHelper.initialize(resultSet2);
when(mockStatement2.executeQuery()).thenThrow(new SQLException(""));
when(mockConnection.prepareStatement(VehicleReadSql.queryGetVehicleByIdAndVersion))
.thenReturn(mockStatement2);
vehicleReadReadDao.getVehicleByIdAndVersion(vehicleId, version);
}
示例5: getVehicleByIdAndVersion_NoCurrent_GoesToHistory_HistoryThrows_ThrowsInternalException
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test(expected = InternalException.class)
public void getVehicleByIdAndVersion_NoCurrent_GoesToHistory_HistoryThrows_ThrowsInternalException()
throws SQLException, InternalException {
final int vehicleId = 890321;
final int version = 23000;
PreparedStatement mockStatement2 = mock(PreparedStatement.class);
when(mockStatement2.executeQuery()).thenThrow(new SQLException(""));
when(mockConnection.prepareStatement(VehicleReadSql.queryGetVehicleHistByIdAndVersion))
.thenReturn(mockStatement2);
when(mockConnection
.prepareStatement(argThat(not(equalTo(VehicleReadSql.queryGetVehicleHistByIdAndVersion)))))
.thenReturn(mockStatement);
vehicleReadReadDao.getVehicleByIdAndVersion(vehicleId, version);
}
示例6: testIterate
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test
public void testIterate() throws SQLException {
String testSql = "SELECT * FROM Sequence";
JdbcDatabase db = spy(new JdbcDatabase(null, null, null));
Connection conn = mock(Connection.class);
doReturn(conn).when(db).getConnection();
PreparedStatement stmt = mock(PreparedStatement.class);
ResultSet thers = mock(ResultSet.class);
when(thers.next()).thenReturn(true, true, true, false);
when(stmt.executeQuery()).thenReturn(thers);
when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
List<Integer> result = db.iterate(testSql, rs -> 17);
verify(conn, times(1)).prepareStatement(testSql);
assertEquals(Arrays.asList(new Integer[] {17, 17, 17}), result);
}
示例7: testSelectAllExceptionThrowed
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Test
public void testSelectAllExceptionThrowed() throws SQLException {
String testSql = "SELECT * FROM Isolate";
JdbcDatabase db = spy(new JdbcDatabase(null, null, null));
Connection conn = mock(Connection.class);
doReturn(conn).when(db).getConnection();
PreparedStatement stmt = mock(PreparedStatement.class);
when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
try {
db.selectAll(testSql, rs -> {
throw new RuntimeException();
});
assertTrue(false);
}
catch (RuntimeException e) {
// pass
}
verify(conn, times(1)).prepareStatement(testSql);
}
示例8: check
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Override
public String check(String id){
Connection conn=null;
String sql=null;
String sql_1=null;
try{
conn=getConnection();
sql="SELECT `meet_id` FROM scada.meet_manager WHERE meet_id="+id;
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
sql_1=rs.getString("meet_id");
}
conn.close();
return sql_1;
}catch(Exception e){
e.printStackTrace();
}
return sql_1;
}
示例9: execute
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Override
public int execute(MapleClient c, String[] splitted) {
if (splitted.length < 2) {
c.getPlayer().dropMessage(0, splitted[0] + " <SQL命令>");
return 0;
}
try {
Connection con = (Connection) DatabaseConnection.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(StringUtil.joinStringFrom(splitted, 1));
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
c.getPlayer().dropMessage(0, "執行SQL命令失敗");
return 0;
}
return 1;
}
示例10: checkIfRowExists
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Override
public boolean checkIfRowExists(int n, double gamma, double C,int total_rows,int training_rows,int run){
try{
String sql = "SELECT id FROM "+this.tablename+" WHERE gamma = ? AND C = ? AND total_rows = ? AND training_rows = ? AND run = ?";
PreparedStatement preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
preparedStatement.setDouble(1, gamma);
preparedStatement.setDouble(2, C);
preparedStatement.setInt(3, total_rows);
preparedStatement.setInt(4, training_rows);
preparedStatement.setInt(5, run);
ResultSet rs = (ResultSet) preparedStatement.executeQuery();
return rs.next();
}catch(SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
System.exit(1);
}
return false;
}
示例11: write
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
@Override
public void write(int n,double gamma, double C,int total_rows,int training_rows,int run,int correct_nbr,double correct_prc) {
try {
String sql = "INSERT INTO "+this.tablename+" (gamma,C,total_rows,training_rows,run,correct_nbr,correct_prc) VALUES (?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
preparedStatement.setDouble(1, gamma);
preparedStatement.setDouble(2, C);
preparedStatement.setInt(3, total_rows);
preparedStatement.setInt(4, training_rows);
preparedStatement.setInt(5, run);
preparedStatement.setInt(6, correct_nbr);
preparedStatement.setDouble(7, correct_prc);
preparedStatement.execute();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
System.exit(1);
}
}
示例12: setRanksByPatternLocation
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
public void setRanksByPatternLocation(String patternKind , String rankQuery, String select_func, int classifierId) throws SQLException
{
int patternType =getPatternType(patternKind);
int pattern_count = getDifferntPatternsCount(patternType , select_func);
if (pattern_count == 0)
throw new SQLException("No rule pattern was found in the database");
float pattern_mul = (1/(float)(pattern_count));
String query = " insert into rulesranks(ruleId,classifierId,rank) " + rankQuery;
logger.info("classifierId= " + classifierId);
logger.info("pattern_mul= " + pattern_mul);
logger.info("patternType= " + patternType);
Connection conn = m_retrivalTool.getMySqlConnection();
PreparedStatement cs = (PreparedStatement) conn.prepareStatement(query);
cs.setInt(1, classifierId);
cs.setFloat(2, pattern_mul);
cs.setInt(3, patternType);
cs.execute();
RetrievalTool.closeConnection();
}
示例13: getNumFromDB
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
/**
* Calls the DB and return a number
* @param parameter1 -- what value to put as a parameter to the query(if null- won't use it)
* @param query -- query to run, the return value will be in column "num"
* @return the res of the query
* @throws SQLException
*/
private int getNumFromDB(String parameter1, String query, String numColumnName) throws SQLException {
Connection conn = m_retrivalTool.getMySqlConnection();
PreparedStatement cs = (PreparedStatement) conn.prepareStatement(query);
if (parameter1 != null)
{
cs.setString(1, parameter1);
}
ResultSet rs = cs.executeQuery();
RetrievalTool.closeConnection();
while (rs.next())
{
return rs.getInt(numColumnName);
}
return (-1);
}
示例14: getDoneBeforeCrashForType
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
public HashSet<Integer> getDoneBeforeCrashForType(int extractionMethodID) throws SQLException {
HashSet<Integer> results = new HashSet<Integer>();
java.sql.Connection conn = getNewtMySqlConnection();
PreparedStatement cs = (PreparedStatement) conn.prepareStatement("SELECT ruleSourceId FROM .rules where ruletype = ? group by ruleSourceId;");
cs.setInt(1, extractionMethodID);
ResultSet rs = cs.executeQuery();
while (rs.next())
{
results.add(rs.getInt(1));
}
conn.close();
return results;
}
示例15: setUp
import com.mysql.jdbc.PreparedStatement; //導入依賴的package包/類
/**
* Configures the mocks and enables the verification.
*
* @throws Exception if an error happens while configuring the mocks.
*/
@Before
public void setUp() throws Exception {
datasource = createMock(DataSource.class);
connection = createMock(Connection.class);
statement = createNiceMock(PreparedStatement.class);
result = createMock(ResultSet.class);
expect(datasource.getConnection())
.andReturn(connection);
expect(connection.prepareStatement(anyString()))
.andReturn(statement)
.anyTimes(); // statement is optional
expect(statement.executeQuery())
.andReturn(result)
.anyTimes(); // executeQuery is optional
statement.close();
connection.close();
verify = true;
}