DIPIKA PANT
DIPIKA PANT

Reputation: 19

How to fix the ERROR PACKAGE com.mysql does not exist

How to fix this program?

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

class MySqlData {
    public static void main(String args[]) throws Exception {
        DriverManager.registerDriver(new com.mysql.jdbcDriver());
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test/user=root&password=merimaa");
        Statement stmt = con.createStatement();
        ResultSet res=stmt.executeQuery("Select * from employee");
        while (res.next()) {
            System.out.println(res.getString("employee_name"));
        }
        con.close();
    }
}

Upvotes: 0

Views: 29209

Answers (5)

xrcwrn
xrcwrn

Reputation: 5327

Add Jar file to WEB-INF/lib.

Use below

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 


 try {
            String connectionURL = "jdbc:mysql://host/db";
            Connection connection = null; 
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            connection = DriverManager.getConnection(connectionURL, "username", "password");
            if(!connection.isClosed())
                 out.println("Successfully connected to " + "MySQL server using TCP/IP...");
            connection.close();
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        }   

https://stackoverflow.com/a/13506117/876739

Upvotes: 0

MMM
MMM

Reputation: 1

You'll need to download the MySQL Connector.

Download it from here: https://dev.mysql.com/downloads/connector/j/

and drop it in your project folder

Upvotes: 0

sdolgy
sdolgy

Reputation: 7001

As other people have put:

Upvotes: 2

somebodyelse
somebodyelse

Reputation: 761

First you need to download a mysql driver, next set up some exception catchers arround the connection with the mysql, or throw some exception.

Upvotes: 0

AlexR
AlexR

Reputation: 115368

Add appropriate jar file that contains sql driver to your classpath

Upvotes: 0

Related Questions