Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

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

Avatar of businessesatoz
businessesatoz asked on
JavaMySQL ServerJava EE
5 Comments1 Solution758 ViewsLast Modified:
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);
                }
        
       
        }

}
ASKER CERTIFIED SOLUTION
Avatar of Pramod Kumar
Pramod KumarFlag of India image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers