java - how do i add a scanner so i can insert the values which are then saved on to mysql -


basically need add scanner input after login in can add id , name etc database don't have use set 1 in program. database gets updated when change set insert values connection fine don't know how user insert values without code update database after entering.

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"; // database name         static final string host = "10.1.63.200"; // database host ip         // full url:         static final string database_url =                  "jdbc:mysql://" + host + "/" + database;     public static void main(string[] args) {         system.out.println("inserting values 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 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 not executed!");                 s.printstacktrace();             }         }         catch (exception e){             e.printstacktrace();         }       } }  

try concatenating string sql using combination of current statement , scanner input. can use preparedstatement , replace 67,\"mark\",\"cooper\" ?,\"?\",\"?\", using preparedstatement's setint() , setstring() methods set values although approach better suited iterating through list.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -