public class JAVADB2 { public static void main(String args[]) throws SQLException { try { Connection conn = null; ResultSet rs = null; Statement stmt = null; String strSQL = "SELECT * FROM employee"; conn = incDB.getConnection(); stmt = (Statement)conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.FETCH_FORWARD); rs = stmt.executeQuery(strSQL); display.getRecords(rs); } catch (SQLException e) { incDB.getException(e); } } } Utility Methodspublic class incDB { private static String USER_NAME = "root"; private static String USER_PASS = "pass"; private static String CONN_STRING = "jdbc:mysql://localhost/test"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(CONN_STRING, USER_NAME, USER_PASS); } public static void getException(SQLException e) { System.out.println("Message :"+e.getMessage()); System.out.println("Error Code :"+e.getErrorCode()); System.out.println("State : "+e.getSQLState()); } } public class incDB { private static String USER_NAME = "root"; private static String USER_PASS = ""; private static String CONN_STRING = "jdbc:mysql://localhost/test"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(CONN_STRING, USER_NAME, USER_PASS); } public static void getException(SQLException e) { System.out.println("Message :"+e.getMessage()); System.out.println("Error Code :"+e.getErrorCode()); System.out.println("State : "+e.getSQLState()); } }
Stored procedure which returns records from select statement
Stored Procedure
DROP PROCEDURE IF EXISTS getEmpDetail; CREATE PROCEDURE getEmpDetail(IN Salary INT) BEGIN SELECT empid, empname, sal FROM employee WHERE sal < Salary; END;
Java Code
public static void main(String args[]) throws SQLException { try { Connection conn = null; ResultSet rs = null; Integer salary = Integer.parseInt(display.getInput("Enter the Salary Amount")); String strSQL = "{CALL getEmpDetail(?)}"; conn = incDB.getConnection(); PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(strSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.FETCH_FORWARD); stmt.setInt(1, salary); rs = stmt.executeQuery(); display.getRecords(rs); } catch (SQLException e) { incDB.getException(e); } }
Utility Function
public class incDB { private static String USER_NAME = "root"; private static String USER_PASS = "pass"; private static String CONN_STRING = "jdbc:mysql://localhost/test"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(CONN_STRING, USER_NAME, USER_PASS); } public static void getException(SQLException e) { System.out.println("Message :"+e.getMessage()); System.out.println("Error Code :"+e.getErrorCode()); System.out.println("State : "+e.getSQLState()); } } public static void getRecords(ResultSet rs) throws SQLException { while(rs.next()) { StringBuffer buffer = new StringBuffer(); buffer.append("Emp Id :"+ rs.getString("empid")+ " Emp Name :"+rs.getString("empname")); System.out.println(buffer.toString()); } if(rs.isBeforeFirst()) System.out.println("Finished Printing"); } public static String getInput(String prompt) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print(prompt); System.out.flush(); try { return stdin.readLine(); } catch (Exception e) { return "Error :"+e.getMessage(); } }
How to Get values from MySQL Stored Procedure in Java that returns more than one resultset
Stored Procedure
DROP PROCEDURE IF EXISTS getEmpDetails; CREATE PROCEDURE getEmpDetails(IN Salary INT, OUT total INT) BEGIN SELECT count(*) into total FROM employee WHERE sal < Salary; SELECT empid, empname, sal FROM employee WHERE sal < Salary; END;
Java Code
public static void main(String args[]) throws SQLException { try { Connection conn = null; ResultSet rs = null; Integer salary = Integer.parseInt(display.getInput("Enter the Salary Amount")); String strSQL = "{CALL getEmpDetails(?, ?)}"; conn = incDB.getConnection(); java.sql.CallableStatement stmt = conn.prepareCall(strSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.FETCH_FORWARD); stmt.setInt(1, salary); stmt.registerOutParameter("total", Types.INTEGER); rs = stmt.executeQuery(); int nRows = stmt.getInt("total"); System.out.println("Total no of Records :"+nRows); display.getRecords(rs); } catch (SQLException e) { incDB.getException(e); } }
Utility Functions
public static void getRecords(ResultSet rs) throws SQLException { while(rs.next()) { StringBuffer buffer = new StringBuffer(); buffer.append("Emp Id :"+ rs.getString("empid")+ " Emp Name :"+rs.getString("empname")); System.out.println(buffer.toString()); } if(rs.isBeforeFirst()) System.out.println("Finished Printing"); }
Query2
The first Location from second table should be added to the first table Location.
CREATE TABLE projectlist(ProjectId INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ProjectName VARCHAR(50), Location VARCHAR(50)); CREATE TABLE LocationList(LocaId INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ProjectId INT, Location VARCHAR(50)); INSERT INTO projectlist(ProjectName) VALUES('Project A'), ('Project B'), ('Project C'), ('Project D'), ('Project E'), ('Project F'), ('Project G'), ('Project H'); INSERT INTO LocationList(ProjectId, Location) VALUES(1, 'Location A'), (1, 'Location C'), (3, 'Location B'), (4, 'Location C'), (5, 'Location D'), (2, 'Location C'), (2, 'Location B'), (2, 'Location A'), (6, 'Location A'), (6, 'Location B'), (7, 'Location B'), (8, 'Location D'), (8, 'Location A');
UPDATE projectlist JOIN (SELECT LocationList.ProjectID, LocationList.Location FROM LocationList JOIN (SELECT LocationList.ProjectID, min(LocaId) minLocaId FROM LocationList GROUP BY LocationList.ProjectID) l1 ON LocationList.LocaId = l1.minLocaID) l2 ON projectList.ProjectID = l2.ProjectID SET ProjectList.Location = l2.Location;
Jquery Dropdown Population after ajax Request
url = "/property/realestate_statistics/"; lngCityId = $('#cboCityName').val(); $.post( url, { "act" : "GetCommonLocation", "CityId" : lngCityId }, function(responseText){ $('#cboLocation').html(responseText); $('#cboPropertyType').html(''); $('#cboPropertyType').append( new Option('Property Type','')); $('#cboPropertyType').val('Property Type'); }, "html" );
I have a response text coming after changing city and this will be populated in second drop down
cboLocation. Now the third drop down should be repopulated based on property type at city by taking common location selected in to consideration. for that you should reset property type apart from resetting cboLocation.
To do so
$('#cboPropertyType').html(''); $('#cboPropertyType').append( new Option('Property Type','')); $('#cboPropertyType').val('Property Type');
- $(‘#cboPropertyType’).html(”) – To empty the Content of Drop down
- $(‘#cboPropertyType’).append( new Option(‘Property Type’,”)) – It should Contain only Property Type in Option so i am Appending that
- $(‘#cboPropertyType’).val(‘Property Type’) – It should point to only one option that exixts
How to escape single quotes in javascript
$strLocation = 'http://www.google.com'; $strLink = "HI there"; (or) $strLink = 'HI there'; print $strLink;
Using ' is html special char which remains undetected during string operations and will appear only in browser.
Using simple escapes the string
how to open link in new tab using javascript
Method 1
function open_in_new_tab(url ) { window.open(url, '_blank'); window.focus(); }
Method 2
$('a').click(function() { $(this).attr('target', '_blank'); });
Method 3
$(document).ready(function() { $('#NewTab').click(function() { $(this).target = "_blank"; window.open($(this).prop('href')); return false; }); });
seo friendly url in php
function seo($input) { //remove single quote and dash $input = str_replace(array("'", "-"), "", $input); //convert to lowercase $input = mb_convert_case($input, MB_CASE_LOWER, "UTF-8"); //replace everything non an with dashes $input = preg_replace("#[^a-zA-Z0-9]+#", "-", $input); //replace multiple dashes with one $input = preg_replace("#(-){2,}#", "$1", $input); //trim dashes from beginning and end of string if any $input = trim($input, "-"); //voila return $input; }
OP:
echo seo(“Tom’s Fish & Chips”); //toms-fish-chips
echo seo(“1-2-3 Pizza”); //123-pizza
How to convert a variable to string in javascript
The toString() method is used for converting variable into string.
var num = 15; var a = num.toString(); var b = num.toString(2); var c = num.toString(8); var d = num.toString(16);
The Results are as Below
15 1111 17 f
How to remove element from javascript array
The below function takes array element which needs to be removed as parameter as below
ary.remove(‘One’); var ary = [‘One’, ‘Two’, ‘Three’]; Array.prototype.remove = function() { var what, a = arguments, L = a.length, ax; while (L && this.length) { what = a[--L]; while ((ax = this.indexOf(what)) !== -1) { this.splice(ax, 1); } } return this; };
The above method works perfectly works for java script array with strings in it.
For the below variable
var csvEmpIds = ‘1,2,3,4,5’; arrEmpIds = csvEmpIds.split(‘,’); ary.remove(2);
The above will not work as you should pass 2 as a string and split function creates
a array by splitting from one variable and putting as a strings in array.
so csvEmpIds.split(‘,’) will create array like below
arrEmpIds = ['1','2','3','4','5'];
In this case to remove element from array which you got by splitting come delimited value you should
do as below.
ary.remove('2');
To Remove array which is declared globally use the below function
var ary = ['three', 'seven', 'eleven']; removeA(ary, 'seven');
function removeA(arr) { var what, a = arguments, L = a.length, ax; while (L > 1 && arr.length) { what = a[--L]; while ((ax= arr.indexOf(what)) !== -1) { arr.splice(ax, 1); } } return arr; }