my dog learned polymorphism*
The moose likes JDBC and the fly likes  jsp mysql connection error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark " jsp mysql connection error" Watch " jsp mysql connection error" New topic
Author

jsp mysql connection error

Akilan Paul
Ranch Hand

Joined: May 22, 2006
Posts: 40
the following code taken from net but it was not working. why? any one help me and also one question how to configure the port no 3306


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

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

<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status</h1>
<%
try {
/* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is usermaster. jdbc:mysql 3306*/
//String connectionURL = "jdbc:mysql://localhost:3306/reg";

//using odbc regdsn already created
String connectionURL = "jdbc:odbc:regdsn";

// declare a connection by using Connection interface
Connection connection = null;

// Load JBBC driver "com.mysql.jdbc.Driver"
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();


/* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL,"root", "root");

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>


Regards,
Aki
Akilan Paul
Ranch Hand

Joined: May 22, 2006
Posts: 40
the following code taken from net but it was not working. why? any one help me and also one question how to configure the port no 3306
i tryed with

Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
these drivers but its not wotking
can you help me


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

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

<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status</h1>
<%
try {
/* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is usermaster. jdbc:mysql 3306*/
//String connectionURL = "jdbc:mysql://localhost:3306/reg";

//using odbc regdsn already created
String connectionURL = "jdbc:odbc:regdsn";

// declare a connection by using Connection interface
Connection connection = null;

// Load JBBC driver "com.mysql.jdbc.Driver"
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();


/* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL,"root", "root");

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

First of all , scriptlet in jsp is discourage. and Coming to your problem , what you took from net [sample code ]is wrong .please download the mysql driver from here and follow the manual

Hope this helps
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221


to work the above code you need to make a Data Source Name(DSN) as this link explains
click here to create a DSN




to make use of this code you need to use a connector jar...download mysql connector

SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

This has nothing to do with JSP, so moving to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp mysql connection error
 
Similar Threads
problem about mysql driver
Only for one data in the text box
Special characters not appearing on JSP page
How to update a data in database from jsp
procedure to establish database connectivity