我曾多次尝试使用预处理语句,但它返回SQL异常.这是我的代码:
public ArrayList<String> name(String mobile, String password) {
ArrayList<String> getdata = new ArrayList<String>();
PreparedStatement stmt = null;
try {
String login = "select mobile, password from tbl_1 join tbl_2 on tbl_1.fk_id=2.Pk_ID where mobile=? and password=?";
String data = "select * from tbl_2 where password='" + password + "'";
PreparedStatement preparedStatement = conn.prepareStatement(login);
preparedStatement.setString(1, mobile);
preparedStatement.setString(1, password);
ResultSet rs = preparedStatement.executeQuery(login);
Statement stmts = (Statement) conn.createStatement();
if (rs.next()) {
System.out.println("Db inside RS");
ResultSet data = stmts.executeQuery(data);
while (data.next()) { /* looping …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Java中的PreparedStatement执行查询。
尝试执行查询时出现错误号1064(语法错误)。
我已经在MySQL查询浏览器中使用替代值测试了此方法,效果很好。
我的代码有什么问题?
以下是相关代码:
String query = "select MemberID, MemberName from members where MemberID = ? or MemberName = ?";
Connection conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
PreparedStatement s = conn.prepareStatement(query);
s.setInt(1, 2);
s.setString(2, "zen");
ResultSet rs = s.executeQuery(query);
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获取在'?附近使用的正确语法。或MemberName =吗?在第1行
我试图将字符串数组和int数组传递给数据库.我正在尝试的代码如下,但这总是显示错误
您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册,以便在第1行的'?,?)'附近使用正确的语法
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/javabase", "java", "password");
CompareAndCount compareAndCount = new CompareAndCount();
ReadWord bagOfWords = new ReadWord();
String[] listOfWords = bagOfWords.openFile();
int[] count =compareAndCount.compareWords();
for (int i = 0; i < listOfWords.length; i++) {
String query = "INSERT INTO angry(tagwords,termfrequency) VALUES(?,?)";
PreparedStatement pStmnt = (PreparedStatement) connection.prepareStatement(query);
pStmnt.setString(1, listOfWords[i]);
pStmnt.setLong(2, count[i]);
pStmnt.executeUpdate(query);
}
connection.close();
Run Code Online (Sandbox Code Playgroud) 我遇到了SQL问题.这是我的代码,我不知道为什么,也不知道修复它.我使用Java EE和Eclipse来做到这一点.
public void userLogin(String username,String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtilsWithProperties.getConnection();
String sql = "select * from users where username = ? and password = ?;";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery(sql);
if(rs.next()) {
System.out.println("SUCESS");
} else {
System.out.println("FAIL");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
JDBCUtilsWithProperties.release(rs, ps, conn);
}
}
}
public class JDBCUtilsWithProperties …Run Code Online (Sandbox Code Playgroud)