0
390views
Database Connectivity in Java with MySql
1 Answer
0
0views

Database Connectivity in Java with MySql

import java.sql.*;

class JdbcMysqlDemo {

    public static void main(String args[]) {

        try {

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

            connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anurag", "root", "root");

            statement stm = con.createStatement();

            resultSet rs = stm.executeQuery("select * from student");

            while(rs.next()) {
                system.out.println(rs.getInt(1)+" "+rs.getString(2));
            }

            con.close();
        }
        catch(Exception e) {
            system.out.println(e);
        }

    }

}

The above program will fetch all the records from student table which is created inside anurag database in mysql.

Note : first you have to create database and table inside mysql database and insert some data into table then run above program.

Please log in to add an answer.

Continue reading...

The best way to discover useful content is by searching it.

Search