How to connect to database connection in Java

I would like to know how to connect to the database that hosts Xampp MySQL.

This is what I still have in my java code to connect, but I'm not sure what I am doing.

public static void main(String[] args) {
    try {
        Connection con = DriverManager.getConnection( host, username, password );
        String host = "jdbc:derby://localhost:1527/Employees";
        String uName = "root";
        String uPass= "password";
    }
    catch ( SQLException err ) {
    System.out.println( err.getMessage( ) );
    }

}
  • What will be the host url?
  • Do I need a JDBC Jar file to connect to the database?

I already installed the database and table through phpMyAdmin. I just don’t know how to act.

I use Netbeans to write Java code to connect to a local database that was created using Xampp PHPMyAdmin .

In the end, I want to create a database connection in Java and invoke tables in the IDE. Thank you for your help.

+3
source share
6 answers

Project structure

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager
        .getConnection("jdbc:mysql://localhost:3306/testspring","root", "password");

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
}

jar Java2s.com

+3

( Java 7, try-with-resources, ) .

 public static final String SELECT_QUERY = "SELECT * FROM your_table_name";
 public static void main(String[] args) {
    String host = "jdbc:derby://localhost:1527/Employees";
    String uName = "root";
    String uPass = "password";

    try (Connection conn = DriverManager.getConnection(host, uName, uPass);
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery(SELECT_QUERY)) {

        while (rs.next()) {
            //read your lines one ofter one
            int id = rs.getInt("id");
            String somePropertyValue = rs.getInt("some_column_name");
            // etc. 
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

, JDBC- (*.jar ) , , , IDE (Eclipse, IDEA ..). ).

BTW, , ? .

+2

, .

URL- .

/lib . %DERBY_HOME%/lib derby.jar, , %DERBY_HOME% - Derby.

0
package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;



public class DatabaseUtil1 {

    public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
    public static final String URL="jdbc:oracle:thin:@DIPAK-PC:1521:xe";
    public static final String USERNAME="system";
    public static final String PASSWORD="password";

    public static Connection getConnection() throws ClassNotFoundException, SQLException
    {
        Connection con= null;

        Class.forName(DRIVER);
        con=DriverManager.getConnection(URL,USERNAME,PASSWORD);

        System.out.println(con);
        return con; 
    }
    public static void closePreparedStatement(PreparedStatement pst) throws SQLException
    {
        if(pst!=null)
        {
            pst.close();
        }

    }
    public static void closeConnection(Connection con) throws SQLException
    {
        if(con!=null)
        {
            con.close();
        }

    }



}
0

: , , JDBC, . import java.sql. * .

JDBC: , .

. Connection, , DriverManager.getConnection().

0

:

, Java- .

β€’ sql

Import.java.sql.*;

β€’

Every database has diffirent driver,we are using sql database so driver for sql database is

Class.forName( "com.mysql.jdbc.Driver" );

β€’ Url

String url="jdbc:mysql://localhost/DataBaseName";

β€’

Connection con=DriverManager(url,"root","");

β€’ Statement

Statement st=con.CreateStatement();

After creating statement object,we can perform sql queries on database,

β€’ Make a request

String sql="SELECT * FROM table_Name ";

β€’ Request ResultSet Object and Excute

ResultSet rs=st.excuteUpdate(sql);

β€’ An important step to close the connection

con.close();

Note , if we don’t have sql connector in jdk, the first boot connector from this url is https://dev.mysql.com/downloads/connector/j/5.1.html and copy and paste,

Java / JRE / Library / ext

0
source

All Articles