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 - UPDATE - update data in Mysql

Programming Date of creation: Views: 213

Mysql Java - UPDATE - update data in Mysql

lesnih.info


Russia

Update UPDATE data in MySQL - Java

In order to update the data in the database Mysql - it is necessary:

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. Create another class in this package mysql_java: update.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 NowUpdate = "UPDATE business SET hits=55777 WHERE id = 2"; // We write the query to the database table itself
        
        update mm = new update();
        String Responce =  mm.update(NowUpdate);
         System.out.print(Responce);
    }
        
}

update.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.
And also, an important point:
In the line:
Responce = Responce + rs.getInt (& - quot; id & -quot;) + & -quot; & -quot; + rs.getInt (& - quot; hits & quot;) + & -quot; \ n & -quot ;;
rs.getInt - if the data type is a number (or replace it with rs.getString, if the data type is a string), specify the name of your fields from your table.

package mysql_java;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
 
 
public class update {
 
    public String update(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 update";
}
 
}

Mysql Java - UPDATE - update data in Mysql
Rating: 5 | Тotal votes: 2 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Post java - Send POST request in Java

    Post java - Send POST request in Java
    Topic: Programming

    Tags: java,post,php,integration,exchange

    Views:326

    Java mail - Send an email from a java application

    Java mail - Send an email from a java application
    Topic: Programming

    Tags: java,email,send email

    Views:368

    Installing Apache Tomcat to work in Java JSP

    Installing Apache Tomcat to work in Java JSP
    Topic: Programming

    Tags: java,jsp,сервер,apache tomcat

    Views:246