Thursday 14 July 2016

SQL SERVER WITH JAVA USING NETBEANS

We follow the following steps :


1.Install SQL Server and  Netbeans.
2.We have to put user name and password while sql server installing.
3.Add Required Sqljdbc jar.

For SQL SERVER


Step 1:    create the database and table in Sql Server Management Studio. 

Step 2:     When we open the  Sql Server Management Studio, have to change the Authentication  name in Sql Server Authentication

Step 3:     Type the Login name and password.Suppose you did not mentioned username and password while your installing,Execute the following query 

                        ALTER LOGIN sa WITH PASSWORD='p@*******';

                         ALTER LOGIN sa enable;

Step 4:      Select  SQL Server Configuration Manager

                  Sql server Network Configuration-->protocols for SQLEXPRESS---->TCP/IP--Enable


For NETBEANS

1. Connect the Sql Server in Driver using sqljdbc jar.

home.jsp

<html>
    <head>
        <title>Login</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="Login">
            Name<input type="text" name="name"><br/>
            Email<input type="email" name="email"><br/>
                        <input type="submit" value="Login">
        </form>
    </body>
</html>


Login.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;            databaseName=Student","sa","Password@12345");
            Statement st=con.createStatement();
            st.executeUpdate("insert into login values('"+name+"','"+email+"')");
            out.println("inserted"); }
        catch(Exception e)
        {
            System.out.println(e); }  } }}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>Login</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

   







No comments:

Post a Comment