Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

how can i have a user input things into mysql using java

Hello, i am trying to create a small program to learn with.. i have created a user input and made my connection to my database good. Although i want to populate my fields with user input using JAVA and MYSQL. so it will ask the user for "First Name" and then insert into first name.  below is my code so far. thank you all for your help.
'import javabook.*;
import java.io.*;

public class bhavdip 
  
{
  


public static void main(String [] args) {

  
        java.sql.Connection conn = null;

        System.out.println("SQL Test");

        try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = java.sql.DriverManager.getConnection(
                        "jdbc:mysql://localhost/people");
 
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                }

        System.out.println("Connection established");
        
//get input statement:
        try{
System.out.print("Enter String:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String data= input.readLine();
System.out.print("You have entered: "+data);
}
catch(Exception e) {
e.printStackTrace();
}


try
{
  java.sql.Statement s = conn.createStatement ();
   int count;
count = s.executeUpdate(
                        "INSERT INTO customers (FirstName,LastName,Phone,Email)"
                        + "VALUES"
                       (data)); 
}  
catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                }

        //this is the sql select statement.
        try {

                java.sql.Statement s = conn.createStatement();
                java.sql.ResultSet r = s.executeQuery ("SELECT FirstName,LastName,Phone  FROM customers");
                while(r.next()) {
                  System.out.println(
                  r.getString("FirstName")+" "+
                  r.getString("LastName")+"  "+
                  r.getString("Phone")
                 );
                        }
          
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                }
        
       
        }

}

Open in new window

Avatar of ChristoferDutz
ChristoferDutz
Flag of Germany image

Could you please explain what is not working?
Avatar of Tomas Helgi Johannsson
           Hi!

This will not work
count = s.executeUpdate(
                        "INSERT INTO customers (FirstName,LastName,Phone,Email)"
                        + "VALUES"
                       (data));

However this will work using Prepared statement
...      
      pstmt = conn.prepareStatement(INSERT INTO customers (FirstName,LastName,Phone,Email) values (?, ?, ?,?)");
      pstmt.setString(1, data_fn); //data_fn string that holds the firstname
      pstmt.setString(2, data_ln);//data_ln string that holds the lastname
      pstmt.setString(3, data_ph);//data_ph string that holds the phonenumber
      pstmt.setString(3, data_em);//data_em string that holds the email
      pstmt.executeUpdate();
...

Links/examples to look at
http://www.java2s.com/Code/Java/Database-SQL-JDBC/MySQL.htm
http://www.java2s.com/Code/Java/Database-SQL-JDBC/PreparedStatement.htm
http://www.java2s.com/Code/Java/Database-SQL-JDBC/CatalogDatabase-SQL-JDBC.htm

Regards,
     Tomas Helgi
Hmmm, yes why this will not work is that your sql - insert statement is wrong. You need to have as many values as there are columns you are inserting.
Like the insert statement int the pstmt class I showed you.
Then if you are only inserting into 1 column of a row and then do several updates where you put the values of the rest of the columns your table columns
need to allow nulls or have default values otherwise "insert into customers(firstname) values(data_fn) will fail.

Regards,
    Tomas Helgi
ASKER CERTIFIED SOLUTION
Avatar of Pramod Kumar
Pramod Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of businessesatoz
businessesatoz

ASKER

thank you, i can understand what you dd here. :)