What does the JDBC ResultSet interface? . . . . For more questions about Java https://bit.ly/465SkSw Check the above link
seen from China

seen from Malaysia

seen from Russia
seen from United Kingdom
seen from United Kingdom

seen from United States
seen from Australia

seen from United States
seen from China
seen from United States

seen from United States
seen from Singapore

seen from United Kingdom

seen from United Kingdom

seen from United Kingdom

seen from Russia
seen from Germany

seen from United States
seen from United States

seen from Italy
What does the JDBC ResultSet interface? . . . . For more questions about Java https://bit.ly/465SkSw Check the above link
Support for variadic parameters list for "in clause" in prepared cql query
今天在工作中遇到这样一个情况:
need to insert an element to list column of cassandra table
起初想用这种形式:
StringBuilder sb = new StringBuilder()
.append("UPDATE txn_promotion SET promotioncodes = promotioncodes + (?) WHERE transactionid = ?;");
但是cassandra 并不支持这种形式, ? 在括号中间不被识别, 应该直接把list<T>作为要输入的参数赋值进去, 以下是两种work的方式:
@Override public void addPromotionCodeByTxnId(String transactionId, String couponCode) { StringBuilder sb = new StringBuilder() .append("UPDATE txn_promotion SET promotioncodes = ['") .append(couponCode) .append("'] + promotioncodes WHERE transactionid = '") .append(transactionId).append("';"); PreparedStatement statement = prepareStatement(sb.toString()); BoundStatement boundStatement = new BoundStatement(statement); boundStatement.setString(1, transactionId); executeInQuorum(boundStatement); return; }
@Override public void addPromotionCodeByTxnId(String transactionId, String couponCode) { try { StringBuilder sb = new StringBuilder() .append("UPDATE txn_promotion SET promotioncodes = promotioncodes + ? WHERE transactionid = ?;"); List<String> list = new ArrayList<String>(); list.add(couponCode); PreparedStatement statement = prepareStatement(sb.toString()); BoundStatement boundStatement = new BoundStatement(statement); boundStatement.setList(0, list); boundStatement.setString(1, transactionId); executeInQuorum(boundStatement); } catch (Exception e) { e.printStackTrace(); } return; }
JDBC PreparedStatement를 사용한 쿼리 실행
/*
- table schema -
CREATE TABLE member{
no number(2),
name varchar2(10),
}
*/
import java.sql.*;
public class PSTMTInsert{
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String dbId = "scott";
String dbPw = "tiger";
Connection conn = null;
PreparedStatement pstmt = null;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,dbId,dbPw);
stmt = conn.createStatement();
String sql = "INSERT INTO member(no, name) VALUES(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 1);
pstmt.setString(2, "ace");
int row = stmt.executeUpdate();
System.out.println(row + "행 추가됨" );
pstmt.setInt(1, 2);
pstmt.setString(2, "ruppy");
int row = stmt.executeUpdate();
System.out.println(row + "행 추가됨" );
}catch(ClassNostFounException e){
// driver로딩 실패...
}catch(SQLException e){
// sql 예외...
}finally{
if(pstmt != null){ try{ pstmt.close(); }catch(SQLException e){} }
if(conn != null){ try{ conn.close(); }catch(SQLException e){} }
}
}
}
먼저 쿼리를 PreparedStatement에 저장한다. 이 쿼리 문자에 바인딩 변수는 ?로 표시한다. ?값을 세팅하기 위해서 setXxx() 메서드를 사용하며, 이 메서드는 위치번호와 위치 표시자에 들어갈 값을 매개변수로 갖는다. 쿼리 문장에 바인딩 변수값을 할당해주었다면 executeUpdate()나 executeQuery() 메서드를 사용해서 쿼리문을 실행하면 된다.
JBDC 데이터베이스 쿼리 실행
Connection 객체를 생성한 후에는 Connection으로 부터 쿼리 실행이 가능한 객체를 생성후 데이터베이스에 쿼리를 실행할 수 있다. 쿼리 실행 클래스에는 Statement, PreparedStatement, CallableStatement 3가지가 있다. 이번장에서는 3가지 클래스의 차이점만 설명하고 각 클래스의 사용방법과 자세한 설명은 뒷단원에서 예제와 함께 설명하겠다.
1. Statement
정적인 쿼리에 사용되며 하나의 쿼리를 사용하고 나면 더 이상 사용할 수 없다.
...
Connection conn = DriverManager.getConnection(url,id,pass);
Stirng name = "ace";
int age = 20;
Statement stmt = conn.createStatement();
String sql = "insert into member values('" +name+ "'," +age+ ")";
int r = stmt.executeUpdate(sql); // 쿼리를 실행할때 입력한다
stmt.close();
2. PreparedStatement
동적인 쿼리에 사용되며 하나의 객체로 여러번의 쿼리를 실행할 수 있다.
sql문장에 정해지지 않은 값을 물음표(?)로 표시하는 바인딩 변수를 사용할 수 있다. 바인딩 변수에 값을 할당하기 위하여 setXxx()메스드를 사용하며, 이 메서드는 물음표의 위치(첫번째 물음표면 1) 값과 실제 할당될 값을 매개변수로 갖는다.
...
Connection conn = DriverManager.getConnection(url,id,pass);
String sql = "insert into member(name, age) values (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql); //쿼리가 실행전에 미리 만들어져야 한다
pstmt.setString(1, "ace");
pstmt.setInt(2, 20);
pstmt.executeUpdate();
// pstmt객체를 재활용도 가능하다
pstmt.setString(1, "ruppy");
pstmt.setInt(2, 18);
pstmt.executeUpdate();
pstmt.close();
3. CallableStatement
데이터베이스 내의 스토어드 프로시저(Stored Procedure)를 호출할 때 사용한다.
Connection conn = DriverManager.getConnection(url,id,pass);
CallableStatement cs = conn.prepareCall("{call 프로시저이름(?,?,?)}");
//입력용
cs.setString(1,"ace");
cs.setInt(2, 20);
//출력용
cs.registerOutParameter(3, java.sql.Types.VARCHAR);
cs.execute();
cs.close();