Monday, 30 November 2015

MySQL Upadate PreparedStatement example

import java.sql.*;

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement stmt = con.prepareStatement("update second set empid=? where rollno=?");
stmt.setInt(1,200);
stmt.setInt(2,1);
stmt.executeUpdate();
con.close();

}catch (SQLException e) {
e.printStackTrace();
}
}

}


output:-

Sunday, 29 November 2015

MySql PreparedStatement to insert rows second example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement stmt = con.prepareStatement("insert into second values (?,?)");
for(int i=0;i<20;i++){
stmt.setInt(1,i);
stmt.setInt(2,i);
stmt.executeUpdate();
}
con.close();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


output:-

MySql PreparedStatment to insert rows


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement stmt = con.prepareStatement("insert into first values (?)");
long t1 = System.currentTimeMillis();
for(int i=1;i<=10;i++){
stmt.setInt(1, i);
stmt.executeUpdate();
}
long t2 = System.currentTimeMillis();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


Output:-

MySql setFetchSize in java



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class First {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Connection con=null;
Statement stmt=null;
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
stmt = con.createStatement();
stmt.setFetchSize(1);
long t1 = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery("select * from first");
while(rs.next()){
System.out.println(rs.getInt(1));
}
long t2 = System.currentTimeMillis();
//System.out.println("The time is");
System.out.println("The time is " +(t2-t1));
con.close();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


output:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
The time is 3

Time taken by MySql Driver to insert 49 values (int values into MYSQL DB)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class First {

//@SuppressWarnings("null")
public static void main(String[] args) {

// TODO Auto-generated method stub

    try {
Connection con=null;
Statement stmt=null;
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
stmt=con.createStatement();
long t1 = System.currentTimeMillis();
for(int j=1;j<50;j++){
stmt.executeUpdate("insert into first values ("+j+")");
}
long t2 = System.currentTimeMillis();
System.out.println("Time is " +(t2-t1));
con.close();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}


Output:-
Time is 21



Friday, 27 November 2015

java program with MySQL to retrieve database records using select


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Second {

public static void main(String[] args) {
// TODO Auto-generated method stub
String query = "select * from persons";
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "root");
stmt = con.createStatement();
ResultSet res = stmt.executeQuery(query);
//System.out.println("No.of Inserted Records == > " + i);

while(res.next()){
System.out.println(res.getInt(1));
System.out.println(res.getString(2));
System.out.println(res.getString(3));
System.out.println(res.getString(4));
System.out.println(res.getString(5));
System.out.println("================");

}

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Output:-
1
sai
prasad
Kasturi Nagr
bangalore
================
2
Prasad
ram
Ram Nagar
Bangalore
================
3
Prasad
ram
Ram Nagar
Bangalore
================
4
babu
ram
Krishna Nagar
Bangalore
================
5
Chaya
Prasad
Banishankari
Bangalore
================
6
Krishna
Prasad babau
Banishankari stage
Mangalore
================

Java program to connect to MYSQL database and insert records


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Second {

public static void main(String[] args) {
// TODO Auto-generated method stub
String query = "insert into persons values ('6','Krishna','Prasad babau','Banishankari stage','Mangalore')";
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "root");
stmt = con.createStatement();
int i = stmt.executeUpdate(query);
System.out.println("No.of Inserted Records == > " + i);

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}



output:-

Monday, 23 November 2015

MYSQL DB Connectivity with java program


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Second {

public static void main(String[] args) {
// TODO Auto-generated method stub
String query = "insert into sample values('sai123')";
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/prasad",
"root", "root");
stmt = con.createStatement();
int i = stmt.executeUpdate(query);
System.out.println("No.of Inserted Records == > " + i);

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Output:-
No.of Inserted Records == > 1


Tuesday, 3 November 2015

Basic HTML

HTML

HTML stands for Hyper Text Markup Language developed to describe the web documents(web pages).It is described by HTML Tags and each tag describes different document content.

HTML Example:-

<!DOCTYPE html>
<html>
<head>
<title>My First Page</title>
</head>
<body>

<h1>My First Heading Line</h1>
<p>My First Paragraph</p>

</body>
</html>