Write a program connectivity with MYSQL Server
Save as a Conn_library.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>conn_library</short-name>
<uri>/WEB-INF/tlds/Conn_library</uri>
<tag>
<name>QueryTag</name>
<tag-class>r4r.co.in.QueryTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>databaseURI</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>password</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>className</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Save as a QueryTag.java
package r4r.co.in;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class QueryTag extends TagSupport {
private Connection con = null;
private String name;
private String databaseURI;
private String password;
private String className;
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setDatabaseURI(String databaseURI) {
this.databaseURI = databaseURI;
}
public void setClassName(String className) {
this.className = className;
}
//Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
Class.forName(className).newInstance();
//(url+databaseName,userName,password)
con = (Connection) DriverManager.getConnection(databaseURI,
name, password);
if (!con.isClosed()) {
out.println("Successfully connected to SQL Server & "
+ " MySQL server using TCP/IP...");
out.println("<BR>Following information from server:");
out.println("<BR>ServerClassName: " + className);
out.println("<BR>ServerDataBase: " + databaseURI);
out.println("<BR>ServerHostName: " + name);
out.println("<BR>ServerPassword: " + password);
out.println("<BR>ServerID: " + id);
} else {
out.println("Sorry, not connect to SQL server");
}
} catch (Exception ex) {
throw new JspException(ex.getMessage());
}
return SKIP_BODY;
}
}
Save as a connection.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/WEB-INF/tlds/Conn_library.tld" prefix="conn_library"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>r4r.co.in</title>
</head>
<body>
<H1 style="font-style: oblique; color: activeborder">
R4R Tech Soft!SQL Connectivity </H1>
<conn_library:QueryTag className="com.mysql.jdbc.Driver"
databaseURI="jdbc:mysql:///r4r"
name="root" password="sachin" />
</body>
</html>
<%-- Used this information--
className= com.mysql.jdbc.Driver
databaseURI= jdbc:mysql:///r4r
password= sachin
name= root
--%>