Download mysql jar and add in your project library. The jar file can be download from below url:
http://mirrors.ibiblio.org/pub/mirrors/maven2/mysql/mysql-connector-java/3.1.12/mysql-connector-java-3.1.12.jar
<%@ page language="java" import="java.sql.*"%>
<%
/*Database connection*/
out.println("MySQL Connect Example.
");
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver"; //or String driver = "org.gjt.mm.mysql.Driver";
String userName = "user";
String password = "pass";
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+dbName,userName,password);
out.println("Connected to the database
");
stmt=con.createStatement();
rst=stmt.executeQuery("Query");
while(rst.next()) {
out.println("
"+rst.getString("field"));
}
con.close();
out.println("
Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
out.println(e);
}
%>
This blog contains code bank related to opensource like PHP, MySQL, Zend Framework, Perl, Linux, CakePHP, Joomla, Drupal, JQuery etc.
Friday, March 16, 2012
Friday, February 17, 2012
MySQL User Privileges
Create User:
CREATE USER username IDENTIFIED BY 'password';
ex:
- CREATE USER myuser IDENTIFIED BY 'mypass';
- CREATE USER myuser;
Assign Permission:
GRANT PRIVILEGES_LIST|ALL ON DATABASE[.TABLES|.*] TO 'username'@'HOST';
ex:
- GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON mydb TO 'myuser'@'localhost';
List Permission:
SHOW GRANTS FOR 'user'@'host';
ex:
SHOW GRANTS FOR 'admin'@'localhost';
CREATE USER username IDENTIFIED BY 'password';
ex:
- CREATE USER myuser IDENTIFIED BY 'mypass';
- CREATE USER myuser;
Assign Permission:
GRANT PRIVILEGES_LIST|ALL ON DATABASE[.TABLES|.*] TO 'username'@'HOST';
ex:
- GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON mydb TO 'myuser'@'localhost';
List Permission:
SHOW GRANTS FOR 'user'@'host';
ex:
SHOW GRANTS FOR 'admin'@'localhost';
Friday, February 3, 2012
MySql Database backup command - mysqldump
Syntax:
mysqldump [options] -h hostname -u username -p password database|--all-databases [tables] > backupfile.sql
Example:
mysqldump [options] -h hostname -u username -p password database|--all-databases [tables] > backupfile.sql
Example:
- mysqldump -h localhost -u root -p test > backup.sql
- mysqldump -h localhost -u root -p test mytable > backup.sql
- mysqldump -h localhost -u root -p --all-databases > backup.sql
Sunday, August 14, 2011
Getting Nth highest salary (will work for all databases)
SELECT * FROM employee e1 WHERE (n-1)=(SELECT COUNT(DISTINCT salary) FROM employee e2 WHERE e2.salary>e1.salary)
*n - Nth highest salary
*n - Nth highest salary
Wednesday, July 20, 2011
How to get Latitude and Longitude of a location
- Open maps.google.com.
- Point the position, which latitude and longitude require.
- Place the below JS code in address bar of your browser.
javascript:void(prompt('',gApplication.getMap().getCenter()));- A popup alert will show you the latitude and longitude of the selected location.
Subscribe to:
Posts (Atom)