Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java connect mariadb

Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/DB?user=root&password=myPassword");
Comment

java connect mariadb


//STEP 1. Import required packages
package mariadb;

import java.sql.*;

public class Mariadb {
    // JDBC driver name and database URL

    static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
    static final String DB_URL = "jdbc:mariadb://192.168.100.174/db";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //STEP 2: Register JDBC driver
            Class.forName("org.mariadb.jdbc.Driver");

            //STEP 3: Open a connection
            System.out.println("Connecting to a selected database...");
            conn = DriverManager.getConnection(
                    "jdbc:mariadb://192.168.100.174/db", "root", "root");
            System.out.println("Connected database successfully...");

            //STEP 4: Execute a query
            System.out.println("Creating table in given database...");
            stmt = conn.createStatement();

            String sql = "CREATE TABLE REGISTRATION "
                    + "(id INTEGER not NULL, "
                    + " first VARCHAR(255), "
                    + " last VARCHAR(255), "
                    + " age INTEGER, "
                    + " PRIMARY KEY ( id ))";

            stmt.executeUpdate(sql);
            System.out.println("Created table in given database...");
        } catch (SQLException se) {
            //Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            //Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            //finally block used to close resources
            try {
                if (stmt != null) {
                    conn.close();
                }
            } catch (SQLException se) {
            }// do nothing
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }//end finally try
        }//end try
        System.out.println("Goodbye!");
    }//end main
}//end JDBCExample

Comment

PREVIOUS NEXT
Code Example
Java :: select class in selenium 
Java :: error: package androidx.multidex does not exist 
Java :: RealmConfiguration.Builder.allowWritesOnUiThread(true) 
Java :: input string a list in java 
Java :: IntStream.generate 
Java :: how to check if a char is equal to int java 
Java :: java input character 
Java :: swing enter key 
Java :: keytool error: java.io.IOException: keystore password was incorrect java.io.IOException: keystore password was incorrect flutter 
Java :: how to solve simultaneous equations in mathematica 
Java :: java coding problems 
Java :: org.mockito.exceptions.misusing.WrongTypeOfReturnValue 
Java :: java loop object 
Java :: how to delete files in java 
Java :: gitignore java 
Java :: abstract vs final 
Java :: android studio close app 
Java :: how to use decimal format java 
Java :: system.out.println shortcut 
Java :: how to find a number in a string java 
Java :: java write 
Java :: How to perform counting sort in Java? 
Java :: java prime 
Java :: create method setsupportactionbar 
Java :: loop list java 
Java :: java resource file 
Java :: java remove duplicates from list by property 
Java :: how to check whether a character is vowel in java 
Java :: sort array based on string length java 
Java :: how to catch enter key in java text field 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =