Scripts: JSP :: database :: Library Article #2

Developer's Section

Create a new Database Connection in JSP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Average Votes: (2782)
Rate It:   

This article explains how to set up a database connection using Mysql and JSP.

In this tutorial we will learn how to create a JSP / Mysql database connection using the com.mysql.jdbc.Driver. The script first defines basic db login information such as username and password. Then it creates an object of type com.mysql.jdbc.Driver. Next you are able to use the driver manager to request a new JDBC connection. This in turn will allow you to execute any type of query on your database.

 Code Snippet 1

<%@ page language="java" session="true" contentType="text/html" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.DateFormat" %>

//define db variables

String dbhost = "";                //your db host address here
String dbname = "MyNewDatabase";     //your db name here
String dbuser = "Myusername";              //your db user here
String dbpass = "Mypassword";              //your db password here

 //create jdbc object

String conStr = "jdbc:mysql://" + dbhost + "/" + dbname;
       conStr = contStr + "?user=" + dbuser + "&password=" + dbpass;

Connection conn = DriverManager.getConnection(conStr); 

Statement sqlStatement = conn.createStatement(); 
String query = "SELECT * FROM Secure_Transaction WHERE username = 'testme'";
ResultSet queryResult = sqlStatement.executeQuery(query); 
if ( {

    visittimes = queryResult.getInt("visittimes");
    lastlogin = queryResult.getString("lastlogin");
    lastaddress = queryResult.getString("lastaddress");
    String pass = queryResult.getString("password");
    out.println("Connection to database Successful");

else {
    out.println("Connection Error");



See other Scripts in database


Submit Your Scripts:

If you would like to have your JSP scripts published in this section please fill out
the form below:
*Your Name or
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]


© 2018 Erobo Software, Inc. All Rights Reserved. | Read Privacy Notice | RSS