我有一個簡單的程序,我想用戶提供的名字和姓氏,然後輸入到MySql數據庫中。這兩個變量,稱爲first
和last
,將是簡單的命令行提示,因此掃描程序首先是掃描程序最後。然後採取第一次和最後一次,並將它們添加到我的MySql語句。這裏是我有的代碼,但由於某些原因,我無法獲得正確的語法。在這個例子中,我使用了語句,儘管我可以並且會使用預準備語句。在現實世界中,我會使用準備好的語句,但即使是一個聲明也適用於這個項目。Java - 發送變量插入數據到MySql數據庫
這是我的代碼與一個掃描儀行。現在,代碼確實與Cathy和Jones的兩個常量值一起工作。我希望這些是變量。
class AddStudent
{
public static void main (String[] args)
{
try
{
Connection conn = DriverManager.getConnection(url,user,pass);
Statement st = conn.createStatement();
Scanner first = new Scanner(System.in);
System.out.println(first.nextLine());
String SQL = "INSERT INTO test VALUES ('Cathy', 'Jones')";
st.executeUpdate(SQL);
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private static String url = "jdbc:mysql://localhost:3306/registrar";
你會得到什麼錯誤? – hvgotcodes