D
D
de_iiah_teji9_iiace2016-01-25 15:27:14
MySQL
de_iiah_teji9_iiace, 2016-01-25 15:27:14

Populating a MySQL Server Java table?

I'm trying to connect to the database and create an entry in the table.
Record creation method:

public void createField(String userId,
                            String userLogin,
                            String userPassword,
                            String userFirstName,
                            String userSecondName,
                            String userParentName,
                            String userPhoneNumber,
                            String userProfession,
                            String administrator) throws SQLException {
        String[] insert = {"insert into users (user_id, user_login,user_password, "
        + "user_first_name, user_second_name, user_parent_name, "
        + "user_phone_number, user_profession, administrator) values "
        + "(" + userId + ", "
        + userLogin + ", "
        + userPassword + ", "
        + userFirstName + ", "
        + userSecondName + ", "
        + userParentName + ", "
        + userPhoneNumber + ", "
        + userProfession + ", "
        + administrator
                +");"};
        try {
            statement = conn.createStatement();
            for(String sql : insert){
                statement.execute(sql);
        }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

Calling a method in main:
DBRequest request = new DBRequest();
        request.connection();
        request.createField("1","1234567890","Qq4443123505", "Andrew", "Makarov", "Valerievich", "0939446910", "IT-specialst", "super");
        DBRequest.closeConnection();

When run it gives:
Connect!
Connected!!
Disconnected
java.sql.SQLException: Unknown column 'Qq4443123505' in 'field list'
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
  at com.mysql.jdbc.Connection.execSQL(Connection.java:3020)
  at com.mysql.jdbc.Connection.execSQL(Connection.java:2949)
  at com.mysql.jdbc.Statement.execute(Statement.java:538)
  at dataBase.DBRequest.createField(DBRequest.java:79)
  at sample.Main.start(Main.java:38)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
  at com.sun.javafx.application.LauncherImpl$$Lambda$50/1232333336.run(Unknown Source)
  at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
  at com.sun.javafx.application.PlatformImpl$$Lambda$46/940634419.run(Unknown Source)
  at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
  at com.sun.javafx.application.PlatformImpl$$Lambda$48/1093519968.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
  at com.sun.javafx.application.PlatformImpl$$Lambda$47/204709364.run(Unknown Source)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
  at com.sun.glass.ui.win.WinApplication$$Lambda$38/139343823.run(Unknown Source)
  at java.lang.Thread.run(Thread.java:745)

Methods for connecting and disconnecting to the database:
public class DBRequest {

    private static Connection conn = null;
    private static Statement statement = null;

    private final String DRIVER;

    private final String USER_ROOT_NAME;
    private final String PASSWORD_ROOT;
    private final String URL;

    public DBRequest(){
        USER_ROOT_NAME = "root";
        PASSWORD_ROOT= "Qq4443123505";
        DRIVER = "com.mysql.jdbc.Driver";
        URL = "jdbc:mysql://localhost/workcalendar";
    }

    public void connection() {
        try{
            Class.forName(DRIVER);
            System.out.println("Connect!");
            try {
                conn = DriverManager.getConnection(URL, USER_ROOT_NAME, PASSWORD_ROOT);
                System.out.println("Connected!!");
            }catch (SQLException e){
                e.printStackTrace();
            }
            }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
public static void closeConnection(){
        if (statement != null){
            try {
                statement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(conn != null)
        {
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        System.out.println("Disconnected");
    }
}

Tell me what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sirs, 2016-01-25
@de_iiah_teji9_iiace

First advice - remake it on PreparedStatement and read about sql injections.
Secondly, in java there are more elegant ways to "glue" a string, for example, the same MessageFormat.format():
String template= "First={0}, second={1}, third={2} parameters.";
String foo = MessageFormat.format(template, "1234567890","Qq4443123505", "Andrew");
The result of foo will be "First=1234567890, second=Qq4443123505, third=Andrew parameters."
And most importantly: if I understand your error correctly, it is related to the fact that you did not wrap the parameters in quotes in the request, i.e. you have a line:
"insert into users (user_id, user_login, user_password, user_first_name, user_second_name, user_parent_name, user_phone_number, user_profession, administrator) values ​​(1, 1234567890, Qq4443123505, Andrew, Makarov, Valerievich, 0939446910, IT-specialst, super);"
but you need:
insert into users (user_id, user_login, user_password, user_first_name, user_second_name, user_parent_name, user_phone_number, user_profession, administrator) values ​​(1, "1234567890", "Qq4443123505", "Andrew", "Makarov", "Valerievich", " 0939446910", "IT-specialst", "super");
Try to add in the createField method:
...
+ "\"" + userLogin + "\", "
+ "\"" + userPassword + "\", "
...
etc. for all String parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question