Results 1 to 4 of 4

Thread: Help me with JSP connecting to database.

  1. #1
    Join Date
    May 2008
    Posts
    20

    Help me with JSP connecting to database.

    Hi,

    I am learning JSP along with MySql as a database application for java.
    I want a sample example if possible with explanation to establish the connection with the database & save records through JSP (Java Server Pages.)

    Please if possible point me to an easy & to the point tutorial or guide on the database connection for JSP.

    Thanks in advance.

  2. #2
    Join Date
    Apr 2008
    Posts
    44

    Re: Help me with JSP connecting to database.

    You need to go through a good tutorial for this.
    All you need is a tomcat server & sdk 1.4 onwards.
    You need to create DNS using ODBC in control panel.
    Here is the co9de you need to write its a snippet to connect to the database.

    Code:
    <%
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:<DSN name>")
    %
    You just need to put the DNS name in the code & you are done.

    I hope this helps.

  3. #3
    Join Date
    Apr 2008
    Posts
    53

    Re: Help me with JSP connecting to database.

    Connect JSP with mysql

    First, you need to create database and then write jsp code to connect jsp to database.

    Create a database:

    Code:
    mysql> create database usermaster;
    (This query create database in my sql command prompt)
    Connect JSP with mysql:

    Code:
    <!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>
    For working example try this:
    Accessing database from JSP

  4. #4
    Join Date
    Apr 2008
    Posts
    33

    Re: Help me with JSP connecting to database.

    Hey have a look at this too:
    Syntax for SQL query in JSP.

    Hope this helps.

Similar Threads

  1. Error in connecting to Internet database
    By Jaganmohini in forum Networking & Security
    Replies: 6
    Last Post: 10-08-2010, 10:37 AM
  2. Connecting C# with MySQL database
    By Quattro in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 10:13 PM
  3. Connecting with SYS and SYSTEM database user
    By Lachlann in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 08:12 PM
  4. Issues in C# application connecting to Remote MySQL 5 database.
    By kyosang in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 04:24 PM
  5. Connecting Oracle Database with ASP dotnet
    By Shashikant in forum Software Development
    Replies: 3
    Last Post: 03-02-2009, 06:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,037,718.50300 seconds with 16 queries