> ## Documentation Index
> Fetch the complete documentation index at: https://kb.mochahost.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL / JDBC connection

Please note that the MySQL connector is NOT loaded by default and you NEED to add any necessary MySQL connectors to your instance in order for your site to connect to MySQL through JDBC

To connect to your MySQL database under Tomcat you can use the JDBC driver which is already included with our Tomcat instance.

You can use JDBC to connect to your MySQL. An example code is provided below:

`driver=com.mysql.jdbc.Driver 
 url=jdbc:mysql://YOUR\_DB\_SERVER\_NAME/yourdatabase 
 userName=your\_mysql\_user 
 password=your\_mysql\_pass`

Below is an example of a simple JSP code working with MySQL via JDBC:

`<%@ page import="java.sql.*" %>   <% 
 String username="your\_mysql\_username"; 
 String password="your\_mysql\_username\_password"; 
 String dbName="your\_complete\_database\_name"; 
 String dbHost="YOUR\_DB\_SERVER\_NAME"; 
 try { 
 Class.forName("com.mysql.jdbc.Driver"); 
 } catch(ClassNotFoundException msg) { 
 out.println("Error loading driver:" + msg.getMessage()); 
  } 
 try{ 
 String url ="jdbc:mysql://"+dbHost+":3306/"+dbName; 
 Connection Conn = DriverManager.getConnection(url,username, password); 
 Statement Stmt = Conn.createStatement(); 
 String query = "select now()"; 
 ResultSet res = Stmt.executeQuery(query); 
 while(res.next())   { 
 out.println("Query result : "+res.getObject(1)); 
 } 
 } catch(SQLException e) { 
 String err1Msg = e.getMessage(); 
 %>    ***err1Msg = <%= err1Msg %>*** |   <% 
 } 
 %>`
