Html 如何在 Javascript 中编写数据库连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19290938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:18:55  来源:igfitidea点击:

How to write a database connection in Javascript?

javascripthtmljspjsp-tags

提问by Vasu

I want to connect to a database depending on the data enter in a .jspform. I do not know how to connect to database in a Javascript.

我想根据.jsp表单中输入的数据连接到数据库。我不知道如何在Javascript.

My code as follows:

我的代码如下:

<%@ 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 here, i want to connect database based on the details entered in the body.

正文在这里,我想根据正文中输入的详细信息连接数据库。

 <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>

回答by Nuwan Dammika

You can not connect to database from client side. JavaScript run on a browser which has a very strict security restrictions. And keeping any connection string on JavaScript is not a good practice at all . Don't keep any sensitive things on client side. Make a call to server side validate and work on that.

您无法从客户端连接到数据库。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.');
            }
            });
    }
}

回答by Vinoth Krishnan

@user2773010 Glad to help, not for entire project. You need to think about how to implement the following snippit as per your requirements.

@ 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);
}
}