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?
4 Answers
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:
User performs and action, raising an event in the browser.
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.A JSP (or better yet, a servlet)
Receives the
POSTmessageValidates the contents, checking for malicious content and other similar attacks
Queries and/or updates the database using prepared statements and similar to avoid SQL injection attacks
Sends the response, with information about the result of the operation
The JavaScript code receives the response to the
POSTajax call and looks at it to see what happened
2 Comments
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
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
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. ;)