当我以用户身份写一个名字时,我需要访问已存在于数据库中的此名称的姓氏。例如:
[{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":{"airplaneDetail":{"flight":
当我执行我的代码时,我看不到任何姓氏作为输出。
在我的数据库中,我有Enter a name: beste
beste's surname is: ozcaglar
,name
和surname
(自动增量)列。
no
答案 0 :(得分:1)
你可以这样说:
import java.sql.*;
import java.util.*;
public class test4 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("Enter a name: ");
String isim = scan.next();
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
System.out.println("Connection success");
String query = "SELECT * FROM users WHERE surname='" + isim + "'";
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(query);
while (rs.next()) {
String fName = rs.getString(1);
String sName = rs.getString(2);
System.out.println("Name: " + fName + " Surname: " + sName);
}
} catch (Exception e) {
System.err.println("Error");
}
}
}
答案 1 :(得分:0)
该错误表示该表没有名为" isim"的列。
也许你打算写:
SELECT surname FROM student_table WHERE name='isim'
编辑(评论后):
query = "SELECT surname FROM student_table WHERE name='" + isim + "'"
但是出于安全原因,它不应该在实际应用程序中使用(它允许SQL注入攻击)。
在实际的生产代码中,您应该转义输入字符串,或使用参数化查询......