Java connect DB by JDBC
import otg.common.*;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.Types;
import java.lang.reflect.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.Types;
import java.lang.reflect.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
public class test1 {
private final static String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final static String URL ="jdbc:sqlserver://" ;
private final static String HOST="nintv520" ;
private final static String PORT ="1433" ;
private final static String DBNAME ="testdb";
private final static String USER = "brainsusr";
private final static String PASSWORD ="usrbrains";
private static String updateSQl="";
private static String delSQL ="";
private static String addSQL="";
private static String flag="";
private final static String FILENAME="D:/gsunJava.txt";
public static void setFlag(String strFlag){
flag =strFlag;
}
public static String getFlag(){
return flag;
}
public static Connection getConnection() throws SQLException, ClassNotFoundException{
Class.forName(DRIVER);
return DriverManager.getConnection(URL+ HOST + ":" + PORT +";databaseName =" + DBNAME ,USER,PASSWORD);
}
public static void methodA(){
System.out.println("It is the first method methodA !"+EnumType.white.toString() );
}
public static void methodB(){
System.out.println("It is the second method methodB !" + EnumTypeName.blue.toString());
}
public static void inputDataToDataBase() throws SQLException, ClassNotFoundException{
Statement stmt =getConnection().createStatement();
String sql = "INSERT INTO TestTable (testcolumn) VALUES('GsunInsert')";
stmt.execute(sql);
System.out.println("done");
private final static String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final static String URL ="jdbc:sqlserver://" ;
private final static String HOST="nintv520" ;
private final static String PORT ="1433" ;
private final static String DBNAME ="testdb";
private final static String USER = "brainsusr";
private final static String PASSWORD ="usrbrains";
private static String updateSQl="";
private static String delSQL ="";
private static String addSQL="";
private static String flag="";
private final static String FILENAME="D:/gsunJava.txt";
public static void setFlag(String strFlag){
flag =strFlag;
}
public static String getFlag(){
return flag;
}
public static Connection getConnection() throws SQLException, ClassNotFoundException{
Class.forName(DRIVER);
return DriverManager.getConnection(URL+ HOST + ":" + PORT +";databaseName =" + DBNAME ,USER,PASSWORD);
}
public static void methodA(){
System.out.println("It is the first method methodA !"+EnumType.white.toString() );
}
public static void methodB(){
System.out.println("It is the second method methodB !" + EnumTypeName.blue.toString());
}
public static void inputDataToDataBase() throws SQLException, ClassNotFoundException{
Statement stmt =getConnection().createStatement();
String sql = "INSERT INTO TestTable (testcolumn) VALUES('GsunInsert')";
stmt.execute(sql);
System.out.println("done");
}
public static void batchHandlerDataToDataBase() throws SQLException, ClassNotFoundException{
Connection con =getConnection();
Statement smt =con.createStatement();
try{
con.setAutoCommit(false);
updateSQl="UPDATE TestTable set testcolumn ='GSUNUpdate' where testcolumn ='GsunInsert'";
delSQL="DELETE TestTable where testcolumn ='C'";
addSQL="INSERT INTO TestTable (testcolumn) values('gsunadd')";
smt.execute(updateSQl);
smt.execute(delSQL);
smt.execute(addSQL);
con.commit();
con.setAutoCommit(true);
}
catch(Exception exc){
con.rollback();
exc.printStackTrace();
}
finally{
con.close();
}
}
public static void invokeStoreProcedure() throws SQLException, ClassNotFoundException{
CallableStatement proc =null;
try{
proc =getConnection().prepareCall("{call testSPName(?,?)}");
proc.setString(1, "A");
proc.registerOutParameter(2,Types.VARCHAR);
proc.execute();
setFlag(proc.getString(2));
if(getFlag().toUpperCase().equals(EnumTypeName.A.toString()))
System.out.println("A present 'Exist' and store procedure return A");
else if(getFlag().toUpperCase().equals(EnumTypeName.B.toString()))
System.out.println("B present 'not exist or null and store procedure return B'");
else
System.out.println("error and store procedure no return any thing");
}
catch(RuntimeException e ){
e.printStackTrace();
System.out.println("error");
}
}
public static void reflectAppRequest(){
try{
Class<?> c = Class.forName("java.util.Stack");
if(c!=null){
System.out.println("--------Method start--------");
Method m[]=c.getDeclaredMethods();
for(int i=0;i<m.length;i++)
System.out.println(m[i].toString());
System.out.println("----------Method end----------");
System.out.println("---------constructor start--------");
Constructor<?> ct[]=c.getConstructors();
for(int j=0;j<ct.length;j++)
System.out.println(ct[j].toString());
System.out.println("----------constructor end ------------");
System.out.println("-------------Field start ---------------");
Field fd[]=c.getDeclaredFields();
for(int p=0;p<fd.length;p++)
System.out.println(fd[p].toString());
System.out.println("----------Field end------------");
}
}
catch(Throwable e){
System.err.print(e);
}
}
public String webservicesRequest(String name){
return "Hello" + name;
}
public static void main(String[] args) throws ClassNotFoundException, InterruptedException, FileNotFoundException{
try {
//inputDataToDataBase();
//batchHandlerDataToDataBase();
invokeStoreProcedure();
reflectAppRequest();
int n=1;
while(true){
System.out.println("===n="+n+"===");
File f1=new File(FILENAME);
writeFile(f1);
long t1=f1.lastModified();
System.out.println("t1="+t1);
Thread.sleep(3000);
File f2 =new File(FILENAME);
writeFile(f2);
long t2 =f2.lastModified();
System.out.println("t2="+t2);
if(t1==t2){
System.out.println("time is the same");
return;
}
n++;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void writeFile(File f) throws FileNotFoundException {
try{
OutputStream o =new FileOutputStream(f);
o.write("10".getBytes());
o.close();
Hashtable<String,Integer> numbers = new Hashtable<String,Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three",3);
Integer u=numbers.get("two");
if(u!=null){
System.out.println("two ="+u);
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}
public static void batchHandlerDataToDataBase() throws SQLException, ClassNotFoundException{
Connection con =getConnection();
Statement smt =con.createStatement();
try{
con.setAutoCommit(false);
updateSQl="UPDATE TestTable set testcolumn ='GSUNUpdate' where testcolumn ='GsunInsert'";
delSQL="DELETE TestTable where testcolumn ='C'";
addSQL="INSERT INTO TestTable (testcolumn) values('gsunadd')";
smt.execute(updateSQl);
smt.execute(delSQL);
smt.execute(addSQL);
con.commit();
con.setAutoCommit(true);
}
catch(Exception exc){
con.rollback();
exc.printStackTrace();
}
finally{
con.close();
}
}
public static void invokeStoreProcedure() throws SQLException, ClassNotFoundException{
CallableStatement proc =null;
try{
proc =getConnection().prepareCall("{call testSPName(?,?)}");
proc.setString(1, "A");
proc.registerOutParameter(2,Types.VARCHAR);
proc.execute();
setFlag(proc.getString(2));
if(getFlag().toUpperCase().equals(EnumTypeName.A.toString()))
System.out.println("A present 'Exist' and store procedure return A");
else if(getFlag().toUpperCase().equals(EnumTypeName.B.toString()))
System.out.println("B present 'not exist or null and store procedure return B'");
else
System.out.println("error and store procedure no return any thing");
}
catch(RuntimeException e ){
e.printStackTrace();
System.out.println("error");
}
}
public static void reflectAppRequest(){
try{
Class<?> c = Class.forName("java.util.Stack");
if(c!=null){
System.out.println("--------Method start--------");
Method m[]=c.getDeclaredMethods();
for(int i=0;i<m.length;i++)
System.out.println(m[i].toString());
System.out.println("----------Method end----------");
System.out.println("---------constructor start--------");
Constructor<?> ct[]=c.getConstructors();
for(int j=0;j<ct.length;j++)
System.out.println(ct[j].toString());
System.out.println("----------constructor end ------------");
System.out.println("-------------Field start ---------------");
Field fd[]=c.getDeclaredFields();
for(int p=0;p<fd.length;p++)
System.out.println(fd[p].toString());
System.out.println("----------Field end------------");
}
}
catch(Throwable e){
System.err.print(e);
}
}
public String webservicesRequest(String name){
return "Hello" + name;
}
public static void main(String[] args) throws ClassNotFoundException, InterruptedException, FileNotFoundException{
try {
//inputDataToDataBase();
//batchHandlerDataToDataBase();
invokeStoreProcedure();
reflectAppRequest();
int n=1;
while(true){
System.out.println("===n="+n+"===");
File f1=new File(FILENAME);
writeFile(f1);
long t1=f1.lastModified();
System.out.println("t1="+t1);
Thread.sleep(3000);
File f2 =new File(FILENAME);
writeFile(f2);
long t2 =f2.lastModified();
System.out.println("t2="+t2);
if(t1==t2){
System.out.println("time is the same");
return;
}
n++;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void writeFile(File f) throws FileNotFoundException {
try{
OutputStream o =new FileOutputStream(f);
o.write("10".getBytes());
o.close();
Hashtable<String,Integer> numbers = new Hashtable<String,Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three",3);
Integer u=numbers.get("two");
if(u!=null){
System.out.println("two ="+u);
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}