基本上我需要添加另一个扫描仪输入,登录后我可以在我的数据库中添加一个 id 和 name 等,这样我就不必在程序中使用集合了。当我只是更改设置的插入值时,数据库会更新,所以连接很好我只是不知道用户如何在没有代码的情况下插入值,然后在输入后更新数据库。
import java.sql.*;
import java.util.Scanner;
public class JDBCInsertValues{
static final String DRIVER = "com.mysql.jdbc.Driver"; // JDBC driver
static final String DATABASE = "test"; // the database name
static final String HOST = "10.1.63.200"; // the database host IP
// the full URL:
static final String DATABASE_URL =
"jdbc:mysql://" + HOST + "/" + DATABASE;
public static void main(String[] args) {
System.out.println("Inserting values into MySQL database table...");
Connection con = null;
try{
Class.forName(DRIVER);
Scanner keyboard = new Scanner(System.in);
System.out.print("Login: ");
String login = keyboard.next();
System.out.print("Password: ");
String password = keyboard.next();
con = DriverManager.getConnection(DATABASE_URL,login,password);
try{
Statement st = con.createStatement();
String sql = "INSERT INTO Authors VALUES(67,\"Mark\",\"Cooper\")";
System.out.println(sql);
int val = st.executeUpdate(sql);
System.out.println(val + " rows inserted.");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
s.printStackTrace();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}