Anda di halaman 1dari 4

<%@ page import = "java.sql.

*"%> <html> <head> <title>Obtaining a Connection </title> </head> <body> <% Connection conn=null; ResultSet result=null; Statement stmt=null; ResultSetMetaData rsmd=null; try { Class c=Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e){ out.write("Error!!!!!!" + e); } try { conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mystudyr_vshare 1", "mystudyr_test","passw0rd"); out.write("Connected!"); } catch(SQLException e) { System.out.println("Error!!!!!!" + e); } %> </body> </html>

Connect JSP with mysql :

Now in the following jsp code, you will see how to connect with the database. First you need to copy my sql connector jar file in lib directory of your application. Name of mysql driver is passed to the forName() method of 'Class' class which loads the driver. To get the connection with the database, getConnection() method of DriverManager class is used which takes three parameters connection url, username and password.

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. */ String connectionURL = "jdbc:mysql://localhost:3306/usermaster"; // declare a connection by using Connection interface Connection connection = null; // Load JBBC driver "com.mysql.jdbc.Driver". Class.forName("com.mysql.jdbc.Driver").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>

Connect JSP with mysql


n this example we will show you how to connect to MySQL database from your JSP code. First, you need to create database and then write jsp code to connect jsp to database. 1. Create a database: First create a database named usermaster in mysql. Before running the jsp code you need to paste mySqlConnector.jar in lib directory of jdk. mysql> create database usermaster; (This query create database in my sql

command prompt) 2. Connect JSP with mysql: Here is the jsp code. ConnectJspToMysql.jsp <!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. */ String connectionURL = "jdbc:mysql://localhost:3306/usermaster"; // declare a connection by using Connection interface Connection connection = null; // Load JBBC driver "com.mysql.jdbc.Driver" Class.forName("com.mysql.jdbc.Driver").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>

Anda mungkin juga menyukai