How to configure Subdomain in Tomcat 7?
Before we jump to solution let me write search queries used by users to do same so they will also get the solution.
- Configure subdomain in apache tomcat 7.
- Configuring Virtual Hosts.
- Configure subdomain in server.xml
- Configure subdomain in DNS.
- Configure subdomain for java.
- Configure subdomain step by step.
- Configure subdomain on www.eatj.com
There is no perfect solution available to configure subdomain. I walk through 200-300 webpages and article to get the solution. I prefer the solution must be understandable. I'll show you how you can configure subdomain(s).
In this example we'll configure subdomain m.domain.com . You can also have your subdomain like a4apple.domain.com . [Note : subdomain name must be lower case you can't give subdomain name as M.domain.com So keep in mind it won't work.]
Step 1: You need to modify the server.xml file available in ${catalina.home}/conf/server.xml or tomcat_installation/conf/server.xml . All you need is append below line of code with in <Engine> Tag.
<Host appbase="webapps/ROOT/m" autodeploy="true" name="m.domain.com" unpackwars="true" xmlnamespaceaware="false" xmlvalidation="false"> <Context docbase="/usr/local/software/tomcat7/webapps/ROOT/m" path="" reloadable="true"></context> <Context antijarlocking="false" antiresourcelocking="false" docbase="/usr/local/software/tomcat7/webapps/manager" path="/manager" privileged="true" reloadable="true"></context> </Host>
Things you need to change:
- appbase : If you have folder where webapps folder is created then appbase will be just m . If folder is with in webapps then appbase will be webapps/m and so on...
- name : change it with your domain name.
- docbase in first context : Set the location of your folder with absolute path.
- Second context : You can remove if you want or set it the the manager folder for your application.
Step 2: Edit DNS record for your website. Search for DNS editor on your website's admin panel. Follow below steps.
- Go to List zones.
- Edit your zones.
- Add record.
- Provide name m.domain.com
- Set Type to A
- Set Content to your Server IP Address.
- Set TTL as per your want or 86400.
Note 1: DNS changes may take sometime if you are working on shared server or ask to your hosting provider.
Note 2: If you are working on windows then search on www.google.com for edit hosts file for subdomain .
Step 3: Restart your Apache Tomcat 7.
Subscribe on Google Plus, Facebook or twitter for latest article...
Subscribe on Google Plus, Facebook or twitter for latest article...
JComboBox (javax.swing.JComboBox) Explained
I generally work around web application. I didn't have much idea about desktop application in Java. I got client wants to create Inventory Management application. I found that its little bit hard to deal with the JComboBox when work around dynamic data. Let me show you how you can hide some of data (i.e database ID, Description, etc...) related to selected item.
Use the function "private void formWindowOpened(java.awt.event.WindowEvent evt)" . I'll give you code snippet that'll help you to understand in better way.
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Vector comboBoxItems = new Vector();
boolean productCount = true;
String itemID = "", name = "", manufacturer = "", stock = "";
try {
Connection con = variables.getDatabaseConnection();
PreparedStatement product_stmt = con.prepareStatement("select * from warehouse");
ResultSet product_rs = product_stmt.executeQuery();
comboBoxItems.add(new Item(0, "Select Item"));
while (product_rs.next()) {
productCount = false;
itemID = product_rs.getString("idwarehouse");
name = product_rs.getString("NameOfProduct");
// To understand below code please read the question 2 in this article
comboBoxItems.add(new Item(Integer.parseInt(itemID), name));
}
if (productCount) {
comboBoxItems.add(new Item(0, "No Item Available"));
}
con.close();
final DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxItems);
jComboBox1.setModel(model);
jComboBox1.addActionListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
2. How to bind the data within JComboBox?
To bind the data for each item we use the below function. That'll also help us to retrieve the data when value of JComboBox is changed or say action performed.
class Item {
private int id;
private String description;
public Item(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public String toString() {
return description;
}
}
3. How to handle event on JComboBox?
Implement the ActionListener for the current class (i.e java file, form, etc...). Modify the line "public class name_of_file extends javax.swing.JFrame" to "public class productStock extends javax.swing.JFrame implements ActionListener". As you are Overriding the ActionListener you have to add that function like below.
@Override
public void actionPerformed(ActionEvent e){
//implement code
}
Now let me show you code snippet that demonstrate the action of change value in JComboBox.
@Override
public void actionPerformed(ActionEvent e) {
/*
* If you are performing action listener for other component then use below code
* if(e.getSource()==jComboBox1){
* Put the below code within IF box
* }
*/
jComboBox1 = (JComboBox) e.getSource();
Item item = (Item) jComboBox1.getSelectedItem();
//Implement your code here
System.out.println("Database ID of selected value"+String.valueOf(item.getId()));
try {
Connection con = variables.getDatabaseConnection();
PreparedStatement warehouse_stmt = con.prepareStatement("select * from warehouse where(idwarehouse=?) limit 0,1");
warehouse_stmt.setString(1, String.valueOf(item.getId()));
ResultSet warehouse_rs = warehouse_stmt.executeQuery();
if (warehouse_rs.next()) {
jTextField1.setText(warehouse_rs.getString("NameOfProduct"));
} else {
JOptionPane.showMessageDialog(rootPane, "Select proper item");
}
con.close();
} catch (Exception exp) {
JOptionPane.showMessageDialog(rootPane, exp.getMessage());
}
}
You have to add all this 3 code in your file in order to work.
- It'll load data when window opened (note: If you want it to load on other action use the code with in function. [i.e : On click, On focus, etc...]).
- It'll bind data in JComboBox.
- It'll handle the change event of JComboBox.
Auto backup MySql database in windows with cloud
Its more important for developer and database admin to preserve data than codding. On a system crash you can code your software again but its hard to get bunch of data available in your database. My system crashed 2 times because of Hard disk error but thanks to cloud saving my ass. If you are new to cloud let me explain you what is cloud and importance of cloud.
Cloud Computing
Cloud Computing is an evolutionary milestone of Internet for your important data. Cloud sync your data from desktop, laptop, tablet, mobile to Internet and vise-versa. When you create file on a local system's cloud folder it'll upload it over Internet ASAP. So if your system crash the next morning you'll have all your important data available over internet and that may save your life.
Cloud Computing service provider
There are so many websites provides cloud computing for free with limited disk space. I listed some good website below. Create your account and folder on local computer.
Note: Don't create cloud folder on C: drive. Sometime only OS of system crash so it may wipe out your cloud folder on re-installation. If your cloud folder contains data of 1GB or more then it'll download all data again so better to just re-install cloud software and point it to old folder.
Creating backup of MySql to cloud folder
Step 1 : Download the javaQuery API [javaQuery Autobackup.bat] from http://www.javaquery.com/p/javaquery-api.html
Step 2 : Copy javaQuery Autobackup.bat in cloud folder say "cloud\database\autobackup program\". Edit file by right click on it > edit. Check out the code snippet of file.
Step 3 : You have to change the file in order to work on your local machine. I listed the data that you have to change in javaQuery Autobackup.bat file
Step up : If you want instant back up just run or double click the file. That will create the back up file at that time.
I created video to demonstrate how you can create the "Schedule Task" for backup.
Cloud Computing
Cloud Computing is an evolutionary milestone of Internet for your important data. Cloud sync your data from desktop, laptop, tablet, mobile to Internet and vise-versa. When you create file on a local system's cloud folder it'll upload it over Internet ASAP. So if your system crash the next morning you'll have all your important data available over internet and that may save your life.
![]() |
| Cloud Computing |
There are so many websites provides cloud computing for free with limited disk space. I listed some good website below. Create your account and folder on local computer.
- Google Drive : https://drive.google.com/
- Dropbox : http://www.dropbox.com/
- Box : http://www.box.net/
Note: Don't create cloud folder on C: drive. Sometime only OS of system crash so it may wipe out your cloud folder on re-installation. If your cloud folder contains data of 1GB or more then it'll download all data again so better to just re-install cloud software and point it to old folder.
Creating backup of MySql to cloud folder
Step 1 : Download the javaQuery API [javaQuery Autobackup.bat] from http://www.javaquery.com/p/javaquery-api.html
Step 2 : Copy javaQuery Autobackup.bat in cloud folder say "cloud\database\autobackup program\". Edit file by right click on it > edit. Check out the code snippet of file.
:: MySql auto backup program by www.javaquery.com :: Its a part of javaQueryAPI :: Follow us on G+: https://plus.google.com/106033350352161272308 ; Facebook:https://www.facebook.com/javaquery.jq ; twitter:https://twitter.com/javaquery :: This program wrote for those who work on local machine database. This will take backup with less options. If you are working with important database and want better options of backup use mysql workbench. set year=%DATE:~10,4% set day=%DATE:~7,2% set mnt=%DATE:~4,2% set hr=%TIME:~0,2% set min=%TIME:~3,2% IF %day% LSS 10 SET day=0%day:~1,1% IF %mnt% LSS 10 SET mnt=0%mnt:~1,1% IF %hr% LSS 10 SET hr=0%hr:~1,1% IF %min% LSS 10 SET min=0%min:~1,1% :: Don't use " in below path. Change file name as you want. set fileName=javaQuery [Date~%day%-%mnt%-%year%;Time~%hr%-%min%].sql :: settings for backup :: Note: Don't use space between "=" and value; i.e : dbuser=root is correct; dbuser= root is wrong. :: userName to login set dbuser=root :: password to login set dbpass=root :: schema to backup; If you want to backup all database use --database for dbschema set dbschema=javaquery :: set host ip address set dbhost=127.0.0.1 :: set port of host set dbport=3306 :: folderpath for backup. Don't use " in below path set fldr=D:\Google Drive\Workspace\Database backup\ :: MySQLdump EXE Path set mysqldumpexe="C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" %mysqldumpexe% --user=%dbuser% --password=%dbpass% --host=%dbhost% --port=%dbport% %dbschema% --routines --result-file="%fldr%%fileName%" echo done... ::pause :: If you find that backup file is not creating remove "::" from above line and it'll show you the execution of code.
Step 3 : You have to change the file in order to work on your local machine. I listed the data that you have to change in javaQuery Autobackup.bat file
- Change fileName ( Backup file name )
- Change dbuser ( Username to login database )
- Change dbpass ( Password to login database )
- Change dbschema ( Schema to create backup )
- Change dbhost ( ipAddress of host )
- Change dbport ( port of host )
- Change fldr ( Folder to store backup file )
- Change mysqldumpexe ( Executable file to create dump of mysql database )
Step up : If you want instant back up just run or double click the file. That will create the back up file at that time.
I created video to demonstrate how you can create the "Schedule Task" for backup.
Code Optimization using javaQuery API
I was wondering why Java(Oracle) group didn't provide useful methods in JDK. I built up some code that may help you to reduce your code. I came up with the solution for below problems that may you also facing.
Optimization class available only in javaQueryAPI 8.0 or above.
- EMPTY AN Array
- The function will allow you to empty only 1-Dimensional Array. Why this function required? To understand run the below code snippet without javaQueryAPI and with javaQueryAPI. If you are thinking that its a static variable but its not the problem. It'll give you same output even if variable is not static.
//Scenario when new String[2] won't work;
public class Demo {
private static String[] temp1 = new String[2];
public static String[] AssignArray(boolean flag) {
if (flag) {
temp1[0] = "pass1";
temp1[1] = "pass2";
}
return temp1;
}
public static void main(String[] args) {
String[] temp = null;
temp = AssignArray(true);
for (String name : temp) {
System.out.println(name);
}
//Optimization : temp = javaQueryBundle.createOptimization().EMPTY_STRING_ARRAY_1D(temp);
temp = AssignArray(false);
System.out.println("--------------------------------------------------------------------");
for (String name : temp) {
System.out.println(name);
}
}
}
/*
* output: without javaQueryBundle
* pass1
* pass2
* ---------------------------------------------------------
* pass1
* pass2
*
* output: with javaQueryBundle (Remove comment : //Optimization :
* pass1
* pass2
* ---------------------------------------------------------
* null
* null
*/
- Count the row in result set
- This class give you total count of Resultset. It may help you when you are dealing with search of database for particular phrase and how many row database have for that phrase. Lets take a look at the code.
import java.sql.*;
import javaQuery.importClass.javaQueryBundle;
public class Demo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("dbc:mysql://localhost:3306/db", "root", "root");
PreparedStatement search_stmt = con.prepareStatement("select * from search where(title like\"%javaQuery%\")");
ResultSet search_rs = search_stmt.executeQuery();
System.out.println(javaQueryBundle.createOptimization().getResultSet_RowCount(search_rs));
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* output
* 10
*/
This is simple as that. I'm also working on other code optimization formula will post ASAP. Thank you all for your contribution. Keep coding...
Note: If you have something and want to put in javaQueryAPI bundle. Contact me on vkijust4u@javaquery.com. I'll publish article along with your name.
Check out the http://www.javaquery.com/p/javaquery-api.html for download.
Getting started with MySQL and Java
MySQL is an opensource Relational Database Management System. Now a days it is used by most of website and software. Apart from that if you are Entrepreneur it helps to cut the cost. I personally use MySQL on my website.
Some of you have any miss guided news about MySQL here is your answer. Facebook uses MySQL. Read more about it on MySQL website : http://www.mysql.com/customers/view/?id=757
Facebook and MySQL statistics :
- Facebook process 60 million request in one second.
- Facebook maintains database of more than 800 million users ; 500 million of them visits facebook daily
- 350 million mobile user
- 7 million application integrated with facebook platform.
- Hope that's enough for you to understand what MySQL can handle...
Lets move to codding part quickly. Before you jump to any code download required software. Follow the links below to quick start :
Database : ( MySQL )
- MySQL Server
- All of our support connectors
- Workbench and sample models ( Provide database access using GUI )
- MySQL Notifier
- MySQL for Excel
- MySQL for Visual Studio
- Sample databases
- Documentation
Library :
- .jar file to connect database
Hope you have installed MySQL in your system. Now let me show you how you can connect to MySQL database with below code snippet.
import java.sql.*; // import SQL connection class
public final class MySQLDemo {
public static final String dbhost = "jdbc:mysql://localhost:3306/javaQuery"; //javaQuery is schema name
public static final String dbusername = "root";
public static final String dbpassword = "root";
public static final String dbdriver = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
try {
Class.forName(dbdriver); // Load driver
Connection con = DriverManager.getConnection(dbhost, dbusername, dbpassword); // Create connection to database
PreparedStatement selectData_stmt = con.prepareStatement("select * from DemoJavaQuery"); // Prepare query
ResultSet selectData_rs = selectData_stmt.executeQuery(); // execute query and get data
while(selectData_rs.next()){ //executes till last row available
System.out.println(selectData_rs.getString("column_name")); // print data
}
/*
* Close connection if you don't need further.
* If you don't close it will create problem when you deal with large application
*/
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
For best practice read more on database:
http://www.javaquery.com/2013/02/preparedstatement-best-practice-of.html
http://www.javaquery.com/search/label/Database
Subscribe to:
Posts (Atom)




