java为什么SQL查询不能直接使用类的实例
我有一个程序,基本上应该将数据上传到本地主机数据库XAMPP。代码正在工作,除了一件事,它没有插入指定的数据。它不读取传入的值,因此在数据库中用0填充这些值。这就是让我困惑的地方。我已经从一个类的实例中很好地选择了变量,但是它没有读取它们。为什么?这些变量不是私有的。我已经尝试在database类上创建该类的实例,但仍然不起作用。我不明白的是为什么主类中的变量不能在数据库类中使用
梅因。爪哇
package com.healthix;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
CollectData instance1 = new CollectData("James", "Minion", "None","Urine Test","Critical",'M',"22/03/1994","14/05/2019",54789631,25,69,true,true);
String firstname = instance1.getFirstName();
String lastname = instance1.getLastName();
String notes = instance1.getNotes();
String test = instance1.getTest();
String refferalReason = instance1.getRefferalReason();
char gender = instance1.getGender();
String dateOfBirth = instance1.getDateOfBirth();
String testDate = instance1.getTestDate();
int nationalId = instance1.getNationalId();
int subjectNumber = instance1.getSubjectNumber();
int result = instance1.getResult();
boolean resultFromAnalyzer = instance1.isResultFromAnalyzer();
boolean acceptAsIs = instance1.isAcceptAsIs();
Database db = new Database();
db.connectDatabase();
}
}
数据库。爪哇
package com.healthix;
import java.sql.*;
public class Database {
public Database() {
connectDatabase();
}
public void connectDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospitalapi", "root", "");
Statement stmt = null;
try {
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
stmt.executeUpdate("insert into Patient(lastname,firstname,notes,test,refferalreason, gender,dateOfBirth,testDate,nationalId,subjectNumber,result,resultFromAnalyzer,acceptAsIs) " +
"values(lastname,firstname,notes,test,refferalReason,gender,dateOfBirth,testDate,nationalId,subjectNumber,result,resultFromAnalyzer,acceptAsIs)");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
收集数据。爪哇
package com.healthix;
public class CollectData {
private String lastName, firstName, notes, test, refferalReason;
private char gender;
private String dateOfBirth, testDate;
private int nationalId = 0, subjectNumber, result;
private boolean resultFromAnalyzer, acceptAsIs;
public CollectData(String lastName, String firstName, String notes, String test, String refferalReason, char gender, String dateOfBirth, String testDate, int nationalId, int subjectNumber, int result, boolean resultFromAnalyzer, boolean acceptAsIs) {
this.lastName = lastName;
this.firstName = firstName;
this.notes = notes;
this.test = test;
this.refferalReason = refferalReason;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.testDate = testDate;
this.nationalId = nationalId;
this.subjectNumber = subjectNumber;
this.result = result;
this.resultFromAnalyzer = resultFromAnalyzer;
this.acceptAsIs = acceptAsIs;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
public String getRefferalReason() {
return refferalReason;
}
public void setRefferalReason(String refferalReason) {
this.refferalReason = refferalReason;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getTestDate() {
return testDate;
}
public void setTestDate(String testDate) {
this.testDate = testDate;
}
public int getNationalId() {
return nationalId;
}
public void setNationalId(int nationalId) {
this.nationalId = nationalId;
}
public int getSubjectNumber() {
return subjectNumber;
}
public void setSubjectNumber(int subjectNumber) {
this.subjectNumber = subjectNumber;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public boolean isResultFromAnalyzer() {
return resultFromAnalyzer;
}
public void setResultFromAnalyzer(boolean resultFromAnalyzer) {
this.resultFromAnalyzer = resultFromAnalyzer;
}
public boolean isAcceptAsIs() {
return acceptAsIs;
}
public void setAcceptAsIs(boolean acceptAsIs) {
this.acceptAsIs = acceptAsIs;
}
}
# 1 楼答案
正如我被告知的那样,我已经签出了PreparedStatement,并意识到我的参数不在SQL语句中。这就是为什么它没有任何东西可以进入数据库。这很有帮助。这是代码。我还必须添加CollectData的实例。将java转换为数据库。JAVA这就是访问main方法中传递的参数的方式。这是最后的代码和有用的资源
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html#create_ps