2013-10-10 7 views
3

데이터에 따라 데이터베이스에 연결하려면 .jsp 양식을 입력하십시오. Javascript에서 데이터베이스에 연결하는 방법을 모르겠습니다.자바 스크립트로 데이터베이스 연결을 작성하는 방법은 무엇입니까?

내 코드를 다음과 같이 여기

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %> 
<html> 
<head> 
<script> 
function validateForm() 
{ 
    if(document.frm.username.value=="srinu") 
{ 
    // conect database here 
} 
else 
{ 
    alert("wrong input"); 
    document.frm.pwd.focus(); 
    return false; 
} 
} 

몸은 내가 본문에 입력 한 세부 정보를 기반으로 데이터베이스를 연결합니다.

<body> 
<form name="frm" method="get" action="validateInput.jsp" onSubmit="return validateForm()"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td width="22%">&nbsp;</td> 
    <td width="78%">&nbsp;</td> 
    </tr> 
    <tr> 
    <td>UserName </td> 
    <td><input type="text" name="username" /></td> 
</tr> 

<tr> 
<td>&nbsp;</td> 
<td><input type="submit" name="submit" value="Submit"></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
+1

자바 스크립트에서이 유효성 검사를해서는 안됩니다. 그것은 좋은 습관이 아닙니다. 이 값을 서블릿에 전달하고 거기에서 유효성을 검사합니다. –

+0

여기에 간단한 예를 들어 줄 수 있습니까? – Vasu

+0

JSP에서 데이터베이스 연결을 Java와 동일하게 만들 수 있습니다. 그러나 좋은 연습이 아니라 JSP 세션의 도움으로 응용 프로그램을 통해 연결을 수행해야합니다. –

답변

4

클라이언트 쪽에서 데이터베이스에 연결할 수 없습니다. JavaScript는 매우 엄격한 보안 제한이있는 브라우저에서 실행됩니다. JavaScript에 연결 문자열을 유지하는 것은 좋은 습관이 아닙니다. 민감한 것을 클라이언트 측에 보관하지 마십시오. 서버 측에 전화를 걸어 유효성을 검사하고 그에 대한 작업을하십시오.

function validateForm() 
{ 
    if(document.frm.username.value=="srinu") 
    { 
     $.ajax({ 
      type: 'GET', 
      url: 'validateuser?username=document.frm.username.value' + '&Pwd=' + document.frm.userpassword.value, 
      success: function (data) {     
       if (data=='Y') 
        alert('Valid User'); 
       else 
        alert('Invalid User'); 
      }, cache: false, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert('failed.'); 
      } 
      }); 
    } 
} 
0

@ user2773010 전체 프로젝트가 아니라 도움이되기를 바랍니다. 요구 사항에 따라 다음 스 니핏을 구현하는 방법에 대해 생각해야합니다.

String userName = request.getParameter("user"); 
String password = request.getParameter("pass"); 

try 
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; 
    con = DriverManager.getConnection("jdbc:odbc:library", "administrator", ""); 
    stat = con.createStatement(); 
    System.out.println("user Connected"); 
    rs = stat.executeQuery("Select * FROM User_Master"); 
    while(rs.next()) 
    { 

    String un = rs.getString(5); 
    String Pwd = rs.getString(6); 

    if(un.equals(userName) && Pwd.equals(password)) 
    { 
     session.setAttribute("username",un); 
     String des = "http://localhost:8080/Final_Review/review/homeuser.jsp"; 
     response.sendRedirect(response.encodeRedirectURL(des)); 
     break; 
    } 
    /* else 
    { 
     response.sendRedirect(response.encodeRedirectURL("http://localhost:8080/Final_Review/review/ErrorPage.jsp")); 
    }*/ 
    } 
    stat.close(); 
    con.close(); 
} 
catch (ClassNotFoundException e) 
{ 
    e.printStackTrace(); 
} 
catch(SQLException se) 
{ 
    System.out.println(se); 
} 
} 
관련 문제