//STEP 1. Import required packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query to create statment with // required arguments for RS example. System.out.println("Creating statement..."); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql; sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); // Move cursor to the last row. System.out.println("Moving cursor to the last..."); rs.last(); //STEP 5: Extract data from result set System.out.println("Displaying record..."); //Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); // Move cursor to the first row. System.out.println("Moving cursor to the first row..."); rs.first(); //STEP 6: Extract data from result set System.out.println("Displaying record..."); //Retrieve by column name id = rs.getInt("id"); age = rs.getInt("age"); first = rs.getString("first"); last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); // Move cursor to the first row. System.out.println("Moving cursor to the next row..."); rs.next(); //STEP 7: Extract data from result set System.out.println("Displaying record..."); id = rs.getInt("id"); age = rs.getInt("age"); first = rs.getString("first"); last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //STEP 8: Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end JDBCExample
Tuesday, March 31, 2015
JAVA JDBC Code
Friday, March 13, 2015
JAVA JDBC
import java.sql.*;
import java.io.*;
class dataDB
{
DataInputStream in =new DataInputStream(System.in);
Connection con;
void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:she","scott","tiger");
System.out.println("Connected");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void insert()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("How many records to insert?");
int n=Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++)
{
System.out.println("Enter studid:");
int si=Integer.parseInt(in.readLine());
System.out.println("Enter stud name:");
String na=in.readLine();
System.out.println("Enter marks:");
int ma=Integer.parseInt(in.readLine());
String query="insert into studs values("+si+",'"+na+"',"+ma+")";
int rows=stmt.executeUpdate(query);
System.out.println("Rows Inserted");
con.commit();
}
}
catch(Exception e1)
{
System.out.println(e1+"Exception insertion");
}
}
void create()
{
Statement stmt;
try
{
stmt=con.createStatement();
String query="create table studs(studid number,name text,marks number)";
int rows=stmt.executeUpdate(query);
System.out.println("Table Created");
}
catch(SQLException e1)
{
System.out.println(e1.getMessage());
}
}
void update()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("Enter studid to update:");
int sid=Integer.parseInt(in.readLine());
System.out.println("Enter new marks:");
int ma=Integer.parseInt(in.readLine());
String query="update studs set marks="+ma+ " where studid="+sid;
int rows=stmt.executeUpdate(query);
System.out.println(rows+"Rows Updated");
con.commit();
}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
void delete()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("Enter studid:");
int si=Integer.parseInt(in.readLine());
String query="delete from studs where studid="+si;
int rows=stmt.executeUpdate(query);
System.out.println(rows+"Rows Deleted");
con.commit();
}
catch(Exception e1)
{
System.out.println(e1+"Exception Deletion");
}
}
void disp()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from studs");
System.out.println("Student Details");
System.out.println("Studid Name Marks");
while(rs.next())
{
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" \n ");
}
}
catch(Exception e1)
{
System.out.println(e1+"Exception insertion");
}
}
public static void main(String ar[])throws IOException
{
DataInputStream in =new DataInputStream(System.in);
dataDB d=new dataDB();
int c;
System.out.println("VARIOUS OPERATION ON DATABASE");
d.connect();
do
{
System.out.println("1.create");
System.out.println("2.insert");
System.out.println("3.delete");
System.out.println("4.modify");
System.out.println("5.display");
System.out.println(" enter ur choice:");
int ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
d.create();
break;
case 2:
d.insert();
break;
case 3:
d.delete();
break;
case 4:
d.update();
break;
case 5:
d.disp();
break;
}
System.out.println("Enter 1->Continue");
c=Integer.parseInt(in.readLine());
}while(c==1);
}
}
import java.io.*;
class dataDB
{
DataInputStream in =new DataInputStream(System.in);
Connection con;
void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:she","scott","tiger");
System.out.println("Connected");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void insert()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("How many records to insert?");
int n=Integer.parseInt(in.readLine());
for(int i=1;i<=n;i++)
{
System.out.println("Enter studid:");
int si=Integer.parseInt(in.readLine());
System.out.println("Enter stud name:");
String na=in.readLine();
System.out.println("Enter marks:");
int ma=Integer.parseInt(in.readLine());
String query="insert into studs values("+si+",'"+na+"',"+ma+")";
int rows=stmt.executeUpdate(query);
System.out.println("Rows Inserted");
con.commit();
}
}
catch(Exception e1)
{
System.out.println(e1+"Exception insertion");
}
}
void create()
{
Statement stmt;
try
{
stmt=con.createStatement();
String query="create table studs(studid number,name text,marks number)";
int rows=stmt.executeUpdate(query);
System.out.println("Table Created");
}
catch(SQLException e1)
{
System.out.println(e1.getMessage());
}
}
void update()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("Enter studid to update:");
int sid=Integer.parseInt(in.readLine());
System.out.println("Enter new marks:");
int ma=Integer.parseInt(in.readLine());
String query="update studs set marks="+ma+ " where studid="+sid;
int rows=stmt.executeUpdate(query);
System.out.println(rows+"Rows Updated");
con.commit();
}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
void delete()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
System.out.println("Enter studid:");
int si=Integer.parseInt(in.readLine());
String query="delete from studs where studid="+si;
int rows=stmt.executeUpdate(query);
System.out.println(rows+"Rows Deleted");
con.commit();
}
catch(Exception e1)
{
System.out.println(e1+"Exception Deletion");
}
}
void disp()throws IOException
{
Statement stmt;
try
{
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from studs");
System.out.println("Student Details");
System.out.println("Studid Name Marks");
while(rs.next())
{
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" \n ");
}
}
catch(Exception e1)
{
System.out.println(e1+"Exception insertion");
}
}
public static void main(String ar[])throws IOException
{
DataInputStream in =new DataInputStream(System.in);
dataDB d=new dataDB();
int c;
System.out.println("VARIOUS OPERATION ON DATABASE");
d.connect();
do
{
System.out.println("1.create");
System.out.println("2.insert");
System.out.println("3.delete");
System.out.println("4.modify");
System.out.println("5.display");
System.out.println(" enter ur choice:");
int ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
d.create();
break;
case 2:
d.insert();
break;
case 3:
d.delete();
break;
case 4:
d.update();
break;
case 5:
d.disp();
break;
}
System.out.println("Enter 1->Continue");
c=Integer.parseInt(in.readLine());
}while(c==1);
}
}
Friday, March 6, 2015
Applet Frame
// Create a child frame window from within an applet.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletFrame" width=400 height=600>
</applet>
*/
// Create a subclass of Frame.
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
// create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
}
public void paint(Graphics g) {
g.drawString("This is in frame window", 10, 40);
}
}
class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}
}
// Create frame window.
public class AppletFrame extends Applet implements ActionListener {
Button okButton;
TextField nameField;
Frame f;
public void init() {
f = new SampleFrame("A Frame Window");
okButton = new Button("Action!");
nameField = new TextField("Type here Something",35);
add(okButton);
okButton.addActionListener(this);
}
public void start() {
}
public void stop() {
f.setVisible(false);
}
public void actionPerformed(ActionEvent evt)
{
// Here we will ask what component called this method
if (evt.getSource() == okButton)
{
f.setSize(150, 150);
f.setVisible(true);
f.add(nameField);
nameField.setText("hello");
//nameField.setText("hello"));
}
}
public void paint(Graphics g) {
g.drawString("This is in applet window", 15, 30);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletFrame" width=400 height=600>
</applet>
*/
// Create a subclass of Frame.
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
// create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
}
public void paint(Graphics g) {
g.drawString("This is in frame window", 10, 40);
}
}
class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}
}
// Create frame window.
public class AppletFrame extends Applet implements ActionListener {
Button okButton;
TextField nameField;
Frame f;
public void init() {
f = new SampleFrame("A Frame Window");
okButton = new Button("Action!");
nameField = new TextField("Type here Something",35);
add(okButton);
okButton.addActionListener(this);
}
public void start() {
}
public void stop() {
f.setVisible(false);
}
public void actionPerformed(ActionEvent evt)
{
// Here we will ask what component called this method
if (evt.getSource() == okButton)
{
f.setSize(150, 150);
f.setVisible(true);
f.add(nameField);
nameField.setText("hello");
//nameField.setText("hello"));
}
}
public void paint(Graphics g) {
g.drawString("This is in applet window", 15, 30);
}
}
Subscribe to:
Posts (Atom)