1

How Do I use javascript to connect and access values of database ? I want to register a user ,so when I am about to create user i want to check wheather username is available or not(means want to do search operation over database)? can we do this with javascript? cause it is use for client side so I am confused?

0

4 Answers 4

2

can we do this with javascript? cause it is use for client side so I am confused?

JavaScript is a general-purpose programming language, not a client-side language. People use it server-side all the time.

However, if you mean, can you use JavaScript in the web browser to access a database on your server, the answer is: Not directly, no (well, not unless you're on an intranet and jump through hoops you really don't want to jump through); you need a middle layer. You can have JavaScript on the client send messages to the server (via ajax, for instance); server-side code then needs to process those messages, ensure they're valid and not malicious, and then very guardedly update the database and send back a result to the client browser.

The server-side language can be JavaScript or any other language you want to use on the server. I notice you tagged your question jsp, so presumably you'd want to write the server-side stuff in Java (rather than JavaScript). If so, and if you want to use ajax so you have a modern, non-page-refresh experience, it looks something like this:

  1. User performs and action, raising an event in the browser.

  2. JavaScript code handles the event by sending an ajax message to the server; in the case of login, you probably want to do a POST.

  3. A JSP (or better yet, a servlet)

    1. Receives the POST message

    2. Validates the contents, checking for malicious content and other similar attacks

    3. Queries and/or updates the database using prepared statements and similar to avoid SQL injection attacks

    4. Sends the response, with information about the result of the operation

  4. The JavaScript code receives the response to the POST ajax call and looks at it to see what happened

Sign up to request clarification or add additional context in comments.

2 Comments

And what if you query the database through REST API using ajax?
@FranciscoSalvador: That changes nothing above. The JSP might well work using RESTful URLs.
2

Directly, not. The JavaScript is generally running on an untrusted machine. Since JavaScript is a scripting language. So, you should consider to use ajax call (e.g. a post) to access your server database...

Similar thread discuss the issue

Comments

0

Thank god it is not possible with JavaScript. You need a server-side scripting language to validate some user´s input against a database.

6 Comments

JavaScript is frequently used as a server-side scripting language.
Yes, but as far as I understand his question, he wants to use JavaScript on client-side!?
Yes, but to say "it is not possible with JavaScript" is to mis-speak. :-)
In my opinion it is fairly unknown that you are actually able to use JavaScript on client-side. Therefor I did not want to confuse him or anything. But you are right :)
(I think you meant server-side.) It used to be less well-known than it is now. Now it seems like NodeJS and similar are popping up everywhere. :-)
|
0

You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:

var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close; 

A better way to connect to a sql server would be to use some server side language like PHP, Java, .NET, among others. Client javascript should be used only for the interfaces.

And there are rumors of an ancient legend about the existence of server javascript, but this is another story. ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.