JAVA SQL 구문 전송 방식

1106 단어 잡회
JAVA 가 SQL 문 구 를 전달 하 는 방식 은 주로 두 가지 가 있 습 니 다. 1. 문자열 '+ 변수 +' 방식 (가장 바깥쪽 은 작은 따옴표 이 고 안 은 작은 따옴표 입 니 다) 2. 자리 차지 문자 '?' 방식 은 두 가지 전달 방식 이 sql 내 장 된 함수 에 도 똑 같이 적 용 됩 니 다.
다음은 시험 예 를 드 리 겠 습 니 다.
ResultSet resultset=null;
//    PreparedStatement  
PreparedStatement stmt=null;
//1.     
int id=1234;
String sql ="SELECT * FROM TABLE WHERE ID='"+id+"' ";
stmt=connection.prepareStatement(sql);
//     
//Timestamp time = new Timestamp(System.currentTimeMillis());
//String sql="SELECT * FROM TABLE WHERE DATEDIFF(DAY,adatetime,'"+time+"')=1";
//stmt=connection.prepareStatement(sql);

//2.     
String id="1234";
String sql ="SELECT * FROM TABLE WHERE ID=?";
stmt=connection.prepareStatement(sql);
stmt.setString(1,id);	//   1   1         ,        
//     
//Timestamp time = new Timestamp(System.currentTimeMillis());
//String sql="SELECT * FROM TABLE WHERE DATEDIFF(DAY,adatetime,?)=1";
//stmt=connection.prepareStatement(sql);
//stmt.setTimestamp(1,time);

resultset=stmt.executeQuery();

좋은 웹페이지 즐겨찾기