Posts

Showing posts from 2011

JDBC Without Creating DSN

&import java.sql.*; public class FirstJDBC { public static void main(String arg[]) { try { /*Registering Driver*/ DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); /*Creating Connection*/ Connection con= DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:database","Username","Password"); Statement st=con.createStatement(); ResultSet result=st.executeQuery("Select * from table_name"); while(result.next()) { System.out.println(result.getInt(1)); System.out.println(result.getString(2)); System.out.println(result.getString(3)); } } catch(Exception expObj) {System.out.print(expObj);} } }

Code for Login & LogOut Using JSP

login.jsp <html> <head> <title>User Login</title> </head> <br> <body Bgcolor ="#0099CC"><hr><hr> <form method="POST" action="sessionAction.jsp"> <p><b>UserName:</b> <input type="text" name="UserName" size="10"></p> <p><b>Password:</b>   <input type="Password" name="Password" size="10"></p> <p><input type="submit" value="Submit" name="submit"><input type="reset" value="Reset" name="reset"></p> </form> </body> </html> sessionAction.jsp <script> function go() { window.location.replace("logout.jsp",'window','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1'); self.close(); } </script...

Display Image Using Label in Swing

import javax.swing.*; import java.awt.*; import java.awt.event.*; class form1 extends JFrame { JLabel l1; form1() { Icon icon2=new ImageIcon("4.JPG"); l2=new JLabel(icon2); l2.setBounds(0,0,600,500); cn.add(l2); setBounds(250,100,600,100); cn.setBackground(Color.BLACK); setSize(600,500); setVisible(true); addWindowListener(this); } public static void main(String arg[]) { new form1(); } }

Menus in Swing using JFrame

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class form2 extends JFrame implements ActionListener { JLabel l1,l2,l3; JTextField t1,t2; JMenuBar menubar; JMenu menu1,menu2,menu3,menu4,menu5,menu6,menu7; JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7,mt8,mt9,mt10,mt11,mt12; form2() { Container cn=getContentPane(); menubar=new JMenuBar(); //menu "Add" added menu1=new JMenu("Add Record"); mt1=new JMenuItem("Customer Record"); mt2=new JMenuItem("Dealer Record"); menu1.add(mt1); menu1.add(mt2); menubar.add(menu1); setJMenuBar(menubar); //menu "Search" added menu2=new JMenu("Search Record"); mt3=new JMenuItem("Customer Search"); mt4=new JMenuItem("Dealer Search"); menu2.add(mt3); menu2.add(mt4); menubar.add(menu2); setJMenuBar(menubar); //menu "Update" added menu3=new JMenu("Update Record"); mt5=new JMenuItem("Customer Up...

Connectivity in VB.NET & ASP.NET

Imports System.Data.OleDb Imports System.Data Public Class Form1 Dim CON As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\db5.mdb")//Database Path Dim DA As New OleDbDataAdapter Dim DS As New DataSet, I As Integer = 0, J As Integer = 0 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CON.Open() DA.InsertCommand = New OleDbCommand DA.InsertCommand.CommandText = "INSERT INTO Table1 VALUES('" & Val(TextBox1.Text) & "','" & TextBox2.Text & "')" DA.InsertCommand.CommandType = CommandType.Text DA.InsertCommand.Connection = CON DA.InsertCommand.ExecuteNonQuery().ToString() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click DA.SelectCommand = New OleDbCommand DA.SelectCommand.CommandText = ...

Java Code to Create MenuBar in Applet

import javax.swing.JApplet; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class MenuApplet extends JApplet { public void init() { JMenuBar menubar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open"); menuFile.add(openItem); JMenuItem saveItem = new JMenuItem("Save"); menuFile.add(saveItem); menubar.add(menuFile); JMenu menuHelp = new JMenu("Help"); JMenuItem aboutItem = new JMenuItem("About"); menuHelp.add(aboutItem); menubar.add(menuHelp); setJMenuBar(menubar); } }

Servlet Code to Display Image from Database

import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DisplayImage extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String connectionURL = "jdbc:odbc:test"; java.sql.Connection con=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); con=DriverManager.getConnection(connectionURL); Statement st1=con.createStatement(); ResultSet rs1 = st1.executeQuery("select image1 from table1 where ID=8"); String imgLen=""; if(rs1.next()) { imgLen = rs1.getString(1); } rs1 = st1.executeQuery("select image1 from table1 where ID=8"); if(rs1.next()){ int len = imgLen.length(); byte [] rb = new byte[len]; InputStream readImg = rs1.getBinaryStream(1); int index=readImg.read(rb, 0, len);...

Servlet Code to Insert Image into Database

import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ImageInsertInTable extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); String connectionURL = "jdbc:odbc:test"; Connection con=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); con = DriverManager.getConnection(connectionURL); PreparedStatement ps = con.prepareStatement("INSERT INTO table1 VALUES(?,?)"); File file = new File("C:/Documents and Settings/MOHSIN/Desktop/WebApplication4/web/Wallpaper.jpg"); FileInputStream fs = new FileInputStream(file); ps.setInt(1,8); ps.setBinaryStream(2,fs,fs.available()); int i = ps.executeUpdate(); pw.print("<h1>"+ i +"</h1>"); if(i!=0){ pw.prin...

Every JSP is a Servlet

Every JSP page is compiled into a servlet by JSP engine. The first time the JSP engine receives the request for a JSP, jsp engine converts the jsp files in to java servlets and than compiles this servlet, it is called the translation phase. If needed JSP pages can also be precompiled. Once the JSP page is compiled into a servlet, all the subsequent requests will be handled by the compiled servlet class. If you modify the source code of the JSP file, container automatically detects the changes and recompile the JSP page the next time that JSP page is requested. Note: If the JSP pages are translated on demand when the container receives the request for an untranslated JSP page, then there will be a slow response for the first time any JSP page is requested. On other hand container may choose to translate the JSP pages at deployment time in that case the first request for any JSP page will take the normal time.

How does JSP differs from Servlets

From the developers perspective Servlets are pure java programs with class and method definitions whereas a JSP page is much like a text document or web page. With servlets developer must write java code to output the entire markup, on the other hand a JSP page can be designed like a static web page. JSP separates static content from dynamic content and Java beans and/or custom JSP tags are used to generate dynamic portion of the web page. Servlets are well suited for handling client request and executing application logic whereas JSP pages are well suited as views.

JSP code for "Display" Info from Database

<%@page language="java" import="java.sql.*"%> <html> <body> <h3>Mohsin Sayad <%!ResultSet rs=null;;Statement st=null;;Connection cn=null; %> <% try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn=DriverManager.getConnection("jdbc:odbc:test"); st=cn.createStatement(); rs=st.executeQuery("select * from BCASTUDENT_TEMP"); while(rs.next()) { out.print(rs.getString(1)); out.print(rs.getString(2)); } } catch(Exception ee) { out.println(ee); } %> </h3>> </body> </html>

Servlet code to insert Record into Database USING Java Client-Server

/*HTML page to accept data(studid,studentname,rollno)*/ <html> <body><center><h1>STUDENT INFORMATION</h1> <form action="http://localhost:8080/DEMO/bcatemp" method="get" name="form1" class="style15" id="form1"><br> Student ID:<input type="text" name="studid" size="30"><br><br> Student Name:<input type="text" name="studnm" size="30"><br><br> Roll no.:<input type="text" name="rno" size="30"><br><br> <input type="submit" name="reset" value=" Submit " onClick=""><br></center><br> </form><br> </center><br> </body><br> </html><br> ------------------------------------------------------------------------------ /*bcatemp.java*/ /*Servlet code to...

Java Code for implementation of Scrollable Resultset(movenext,moveprevious,movefirst,movelast)

import java.io.*; import java.sql.*; import java.awt.*; import java.awt.event.*; class slip2 extends Frame implements ActionListener { Button b1,b2,b3,b4; TextField t1,t2,t3,t4; Label l1,l2,l3,l4; Connection con=null; Statement st=null; ResultSet rs=null; slip2() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:emp1"); st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=st.executeQuery("select * from emp"); } catch(Exception ee) { System.out.println("fsfsddsf"+ee); } l1=new Label("Enter No"); l2=new Label("Enter Name"); l3=new Label("Enter Salary"); t1=new TextField(); t2=new TextField(); t3=new TextField(); b1=new Button("First"); b2=new Button("Last"); b3=new Button("Next"); b4=new Button("Previous"); setLayout(null); add(l1); l1.setBou...

How To Hide Folder Without Using Software

STEP-1:GO TO RUN START->RUN STEP-2:WRITE COMMAND ATTRIB (ENTIRE PATH OF FOLDER) +S +H +R EG:ATTRIB C:\A +S +H +R PRESS OK BUTTON STEP-3:(TO SHOW FOLDER) GO TO RUN START->RUN WRITE COMMAND ATTRIB (ENTIRE PATH OF FOLDER) -S -H -R EG:ATTRIB C:\A -S -H -R PRESS OK BUTTON