| 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: 56150
|
|
|
This has nothing to do with JSP, so moving to the JDBC forum.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: jsp mysql connection error
|
|
|