jsp中sql引用变量的问题
String condition= "INSERT INTO login VALUES "+ "( "+ " ' "+id+ " ', "+passowrd+ ") ";
比如象这一句,正确写法应该是什么样的
", ',+正确用法
------解决方案--------------------lz这样写当然没有问题。
但是建议lz使用PreparedStatement来做sql。
package org.luyang.jdbc;
import java.sql.*;
class Test {
Connection con;
PreparedStatement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public Test() {
driver = "oracle.jdbc.driver.OracleDriver ";
url = "jdbc:oracle:thin:@ws2d-zm2:1521:zmit ";
user = "kaikei2 ";
pwd = "kaikei2 ";
init();
}
public void init() {
try {
Class.forName( "oracle.jdbc.driver.OracleDriver "); // 驱动程序加载
con = DriverManager.getConnection(url, user, pwd); // connection 连接
sta = con
.prepareStatement( "INSERT INTO login VALUES (?, ?)); // PreparedStatement 创建
int seq = 1;
sta.setString(seq++, "username "); // 对 ?1 赋值
str.setLong(seq++, password); // 对 ?2 赋值
sta.executeUpdate();
con.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class DBTest {
public static void main(String[] args) {
new Test();
}
}
===========================
当然现实中对怕ssword的处理绝对不会这么简单。
------解决方案--------------------字符串的两端应该加单引号
所以,如果你的id和password都是字符串的话,应该
String condition= "INSERT INTO login VALUES "+ "( "+ " ' "+id+ " ', ' "+passowrd+ " ') ";
------解决方案-------------------- String condition= "INSERT INTO login VALUES ( "+id+ ", "+passowrd+ ") ";
变量不需要加单引号