menu


Subscribe to the blog

Social networks

My page on vk.com
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Mysql Java - INSERT - add data to Mysql

Programming Date of creation: Views: 217

Mysql Java - INSERT - add data to Mysql

lesnih.info


Russia

INSERT Add data to MySQL - Java

In order to add data to the Mysql database, you must:

1.Create a project in the IDE, for example with the name mysql_java (Automatically creates a package mysql_java, and in it the main class: Mysql_java.java)

2. We create another class in this package mysql_java: insert.java.

3. Load the library with a similar name in the project library mysql-connector-java-8.0.12.jar If you have already loaded the library into the project, then you do not need to load it again.

You can download the library here:https://dev.mysql.com/downloads/connector/j/

And now, the code for the classes is:

Mysql_java.java

package mysql_java;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Mysql_java {
    
         public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException  {
        
       

String Nowinsert = "INSERT INTO `url` (`id`, `tab`, `url`) VALUES (NULL, '1', 'link555') ";// We write the query itself to the database table, enumerate all the fields in the table and the values for these fields
        
        insert mm = new insert();
        String Responce =  mm.insert(Nowinsert);
         System.out.print(Responce);
    }
        
}

insert.java

A comment: settings for connecting to the database in this example:
host: "localhost"
Database: "corp"
username: "root"
password: "1234567"
Accordingly, the search can find where exactly these connection settings are placed in the code and change them to their own. The

package mysql_java;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
 
 
public class insert {
 
    public String insert(String sql) throws  SQLException
 
            Connection con = null;
            PreparedStatement prep = null;
 
            try
            {
                Class.forName("com.mysql.cj.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/corp?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "1234567");
               prep = con.prepareStatement(sql);
                prep.executeUpdate();
                prep.close();
 
            } 
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
 
            }
            return "successfully insert";
}
 
}

Mysql Java - INSERT - add data to Mysql
Rating: 5 | Тotal votes: 2 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Java XML - XML record in a java application

    Java XML - XML record in a java application
    Topic: Programming

    Tags: java,xml,write xml,integration,exchange

    Views:1261

    How to send email to JSP Java

    How to send email to JSP Java
    Topic: Programming

    Tags: java,jsp,send email,email

    Views:602

    Comparison of methods and functions in Javascript, Java, PHP

    Comparison of methods and functions in Javascript, Java, PHP
    Topic: Programming

    Tags: java,javascript,php

    Views:226