Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
jsp MySQL database connectivity Shanu k k shanukk89@gmail.com www.facebook.com/shanunn i twitter.com/shanu in.linkedin.com/in/shanu k k 9656153432
Typing Speed Week Target Achieved 1 25 25 2 30 27 3 29 30
Jobs Applied # Company 1 www.noukri.com 2 www.shine.com 3 www.fresherworld.c om Designation Applied Date Current Status
Accessing data in a database or in other data sources is an important task in web programming. data access from JSPs is done through Java Database Connectivity (JDBC). We will begin with an introduction to JDBC
Here are the steps required to access data in a database: Load the JDBC database driver. Create a connection. Create a statement. Create a resultset, if you expect the database server to send back some data.
 There are two packages in JDBC • java.sql • javax.sql The javax.sql package is the JDBC Optional Package
DriverManager Class • The DriverManager class is used to obtain a connection to a database. • Database servers use their own proprietary protocols for communication, which are different from each other. However, we don't need to worry about these protocols because we can use "translators." These "translators" come in the form of JDBC drivers • For an jDBC database, use the following code to load the driver: Class.forName("com.mysql.jdbc.Driver");
The Connection Interface • To access a database, first you need to establish a connection to the database server. • Connection connection = DriverManager.getConnection(url, "root", "baabtra"); • The most frequently used method of the Connection interface is createStatement(), which returns a Statement object for sending SQL statements to the database.
Statement Interface the Statement interface method to execute an SQL statement and obtain the produced results. Statement statement = connection.createStatement(); The two most important methods of this interface are executeQuery() and executeUpdate(). The executeUpdate() method executes an SQL INSERT, UPDATE, or DELETE statement The executeQuery() method executes an SQL SELECT statement that returns data.
• <%@ page language="java" contentType="text/html; charset=ISO-8859-1" • pageEncoding="ISO-8859-1"%> • <%@ page import ="java.sql.*" %> • <%@ page import ="javax.sql.*" %> • <html> • <head> • <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> • <title>Front</title> • </head> • <body> • <%
<% String name=request.getParameter("name"); String password= request.getParameter("pass"); String adress = request.getParameter("address"); String dob=request.getParameter("dob"); String url = "jdbc:mysql://localhost:3306/db_tes"; if(name!="" && password!="" && adress!="" && dob!="") { try { String insert = "INSERT INTO tbl_adress(name,password,adress,DOB)" + "VALUES (? ,?, ?, ?)"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url, "root", "baabtra"); PreparedStatement ps = con.prepareStatement(insert);
ps.setString(1,name); ps.setString(2,password); ps.setString(3,adress); ps.setString(4,dob); ps.executeUpdate(); con.close(); } catch (Exception ex) { out.println("error"); } } %>
<form action="registration form.jsp" method="POST"> <table> <tr> <br> <td>Name</br></td> <td> <input type="text" name="name"> </td> </tr> <tr> <br><td>Password</br></td> <td> <input type="password" name="pass"> </td> </tr>
• • • • • • • • • • • • • • • • <tr> <br><td>date of birth</br></td> <td> <input type="text" name="dob"> </td> </tr> <tr> <br><td>Adress</br></td> <td> <textarea name="address"></textarea> </td> </tr> <br> <tr> <td><input type="submit" value="register"></td> </tr>
• • • • </table> <br> <hr> Already Registered!! To Login <a href="login.jsp">Click Here</a> • <hr> • </form> • </body> • </html>
If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

jsp MySQL database connectivity

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
    jsp MySQL databaseconnectivity Shanu k k shanukk89@gmail.com www.facebook.com/shanunn i twitter.com/shanu in.linkedin.com/in/shanu k k 9656153432
  • 4.
  • 5.
  • 6.
    Accessing data ina database or in other data sources is an important task in web programming. data access from JSPs is done through Java Database Connectivity (JDBC). We will begin with an introduction to JDBC
  • 7.
    Here are thesteps required to access data in a database: Load the JDBC database driver. Create a connection. Create a statement. Create a resultset, if you expect the database server to send back some data.
  • 8.
     There aretwo packages in JDBC • java.sql • javax.sql The javax.sql package is the JDBC Optional Package
  • 9.
    DriverManager Class • TheDriverManager class is used to obtain a connection to a database. • Database servers use their own proprietary protocols for communication, which are different from each other. However, we don't need to worry about these protocols because we can use "translators." These "translators" come in the form of JDBC drivers • For an jDBC database, use the following code to load the driver: Class.forName("com.mysql.jdbc.Driver");
  • 10.
    The Connection Interface •To access a database, first you need to establish a connection to the database server. • Connection connection = DriverManager.getConnection(url, "root", "baabtra"); • The most frequently used method of the Connection interface is createStatement(), which returns a Statement object for sending SQL statements to the database.
  • 11.
    Statement Interface the Statementinterface method to execute an SQL statement and obtain the produced results. Statement statement = connection.createStatement(); The two most important methods of this interface are executeQuery() and executeUpdate(). The executeUpdate() method executes an SQL INSERT, UPDATE, or DELETE statement The executeQuery() method executes an SQL SELECT statement that returns data.
  • 12.
    • <%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1" • pageEncoding="ISO-8859-1"%> • <%@ page import ="java.sql.*" %> • <%@ page import ="javax.sql.*" %> • <html> • <head> • <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> • <title>Front</title> • </head> • <body> • <%
  • 13.
    <% String name=request.getParameter("name"); String password=request.getParameter("pass"); String adress = request.getParameter("address"); String dob=request.getParameter("dob"); String url = "jdbc:mysql://localhost:3306/db_tes"; if(name!="" && password!="" && adress!="" && dob!="") { try { String insert = "INSERT INTO tbl_adress(name,password,adress,DOB)" + "VALUES (? ,?, ?, ?)"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url, "root", "baabtra"); PreparedStatement ps = con.prepareStatement(insert);
  • 14.
  • 15.
    <form action="registration form.jsp"method="POST"> <table> <tr> <br> <td>Name</br></td> <td> <input type="text" name="name"> </td> </tr> <tr> <br><td>Password</br></td> <td> <input type="password" name="pass"> </td> </tr>
  • 16.
    • • • • • • • • • • • • • • • • <tr> <br><td>date of birth</br></td> <td> <inputtype="text" name="dob"> </td> </tr> <tr> <br><td>Adress</br></td> <td> <textarea name="address"></textarea> </td> </tr> <br> <tr> <td><input type="submit" value="register"></td> </tr>
  • 17.
    • • • • </table> <br> <hr> Already Registered!! ToLogin <a href="login.jsp">Click Here</a> • <hr> • </form> • </body> • </html>
  • 19.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 20.
    Contact Us Emarald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550