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 - SELECT data from Mysql - Java

Programming Date of creation: Views: 230

Mysql Java - SELECT data from Mysql - Java

lesnih.info


Russia

Select - Get data from MySQL in Java

In order to connect 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. Create another class in this package mysql_java: select.java.

3. Load a library with a similar library name into the project, for example:  mysql-connector-java-8.0.12.jar

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 GetSelect = "select * from business WHERE id=2"; // Write the query itself to the database table
        
        select mm = new select();
        String Responce =  mm.main(GetSelect);
         System.out.print(Responce);
    }
        
}

select.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
And also, an important point:
In the line:
Responce = Responce + rs.getInt("id") + " " + rs.getInt("hits")+  "\n";
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class select {
public Connection Connect;
public Statement stmt;
public select() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
 
Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); 
this.Connect = DriverManager.getConnection("jdbc:mysql://localhost/corp?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "1234567");
this.stmt = this.Connect.createStatement();
}
 
public ResultSet  request(String select) throws  SQLException
{           
            ResultSet rs = this.stmt.executeQuery(select);
            return (rs); 
}
 
        
       
        
        public static String main(String select) throws SQLException  {
        
         String Responce = "";
       
           select BDCatalogElement = null;
            try {
                BDCatalogElement = new select();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(select.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(select.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(select.class.getName()).log(Level.SEVERE, null, ex);
            }
           ResultSet rs = BDCatalogElement.request(select);
            
            while (rs.next()) 
                {       
                        Responce = Responce + rs.getInt("id") + " " + rs.getInt("hits")+  "\n";
 
}
            
        
        return Responce;
    }
    
}

Mysql Java - SELECT data from Mysql - Java
Rating: 5 | Тotal votes: 2 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Java - TXT Read and write a text document

    Java - TXT Read and write a text document
    Topic: Programming

    Tags: java,txt,write txt,read txt,integration,exchange

    Views:281

    Post java - Send POST request in Java

    Post java - Send POST request in Java
    Topic: Programming

    Tags: java,post,php,integration,exchange

    Views:326

    Arithmetic Operators in Java

    Arithmetic Operators in Java
    Topic: Programming

    Tags: java

    Views:619