Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://192.168.100.100:3306/";
        String dbName = "databaseName";
        Statement stmt = null;
        ResultSet result = null;
        String driver = "com.mysql.jdbc.Driver";
        String databaseUserName = "admin";
        String databasePassword = "root";
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword);
        stmt = conn.createStatement();
        result = null;
        String password, username;
        result = stmt.executeQuery("select * from userTable where username ='user1' ");
        if (!result.isBeforeFirst()) {
            System.out.println("resultset contin no rows");
        }
        while (result.next()) {
            username = result.getString("username");
            password = result.getString("password");
            System.out.println(username + "  " + password);
        }
        conn.close();
    }
}