Reputation: 19
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
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
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
Reputation: 7001
As other people have put:
Upvotes: 2
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
Reputation: 115368
Add appropriate jar file that contains sql driver to your classpath
Upvotes: 0