System.out.println("请输入ID:");
String i = br.readLine();
System.out.println("请输入姓名:");
String n = br.readLine();
............
try {
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
//从数据库连接获得一个查询窗口
st = dbConn.createStatement();
st.execute("insert into engineer(id, name) values ('i', 'n'));
System.out.println("连接数据库成功!");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.print("SQL Server连接失败!");
}
如果i输入: 0001,n输入:张三
但是这样写只是插入了“i”和“n”两个变量。
------解决方案--------------------
st.execute("insert into engineer(id, name) values ('i', 'n'));
改为
st.execute("insert into engineer(id, name) values ('"+ i + "', '"+ n + "'));