2013-01-17 4 views
2

나는 html 파일을 String으로 가지고 있는데, 나는 그것을 업데이트 쿼리를 사용하여 MySQL DB에 삽입하고 싶다. - 아마 난 그냥 인용 HTML ""으로 기운 그것은 또한 많은 특수 문자와 따옴표를 포함로 삽입JDBC를 사용하여 MySQL DB에 html 파일을 저장하는 방법은 무엇입니까?

MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'zglosBladWindow').ck_window.showCenter('this');" href="#">zg?o? b??d na stronie<' at line 1 

이 HTML 내부에 somwhere입니다 :하지만 예외 다음 얻을

Statement st = connection.createStatement(); 
String query = "UPDATE orders SET status='ready', html='"+html+"' WHERE id='123'"; 
int num = st.executeUpdate(query); 

: 나는이 시도 - 내가 어떻게 그것을 삽입 할 수 있니? 어떻게 든 그것을 인코딩해야합니까?

+0

당신은 변수'html'을 탈출해야합니다. – hjpotter92

답변

7

진술보다는 PreparedStatement을 사용하도록 조언드립니다.

String query = "UPDATE orders SET status=?, html=? WHERE id=?"; 
PreparedStatement stmnt = conn.PreparedStatement(query); 
stmnt.setString(1, yourtext); 
.... 
int num = st.executeUpdate(); 
+1

고마워,이게 내가 찾고있는 것이지만 코드에 약간의 실수가있다 :'int num = st.executeUpdate (query); 대신'int num = st.executeUpdate();' – user606521

+0

@ user606521이 있어야한다. 당신은 환영합니다. 그리고 그것을 지적 해 주셔서 고마워요, 그것은 기본적으로 복사/붙여 넣기였습니다 .. :) – PermGenError

0

HTML 문자열에 따옴표 또는 이중 qout가 포함될 수 있으므로 PreparedStatement 구문을 사용해야합니다. 더 읽기 here

0

PreparedStatement를 사용해야하지만 파일을 저장하는 대신 MySQL에서 경로를 저장하고 싶습니다. 당신은 아마이 같은 것을 사용한다

0

,

StringBuilder contentBuilder = new StringBuilder(); 
try { 
BufferedReader in = new BufferedReader(new FileReader("mypage.html")); 
String str; 
while ((str = in.readLine()) != null) { 
    contentBuilder.append(str); 
} 
in.close(); 
} catch (IOException e) { 
} 
String content = contentBuilder.toString(); 
관련 문제