Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package template1;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author Manas9
*/
public class Template1 {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// TODO code application logic here
System.out.println("main block executing ");
Connection c = null; Statement st = null;
ResultSet rs = null; // result set object
String dbURL = "jdbc:mysql://localhost/test?";
String user = "Manas9"; String pwd = "Manas9237";
Properties param = new Properties();
// connecting to db
try {
//replacing Class.forName("org.postgresql.Driver");
param.put("user",user); param.put("password",pwd);
c = DriverManager.getConnection(dbURL, param);
st= c.createStatement();
System.out.println("Connected to database successfully");
/*
String table = "CREATE TABLE emp5 (" +
"EMPNO mediumint(2 ) NOT NULL,\n"+
"ename char(10) DEFAULT NULL,\n"+
"JOB char(9 ) DEFAULT NULL,\n"+
"MGR mediumint(4 ) DEFAULT NULL,\n"+
"HIREDATE date DEFAULT NULL,\n"+
"SAL mediumint(9) DEFAULT NULL,\n"+
"COMM mediumint(9 ) DEFAULT NULL,\n"+
"DEPTNO mediumint(2) NOT NULL,\n"+
"PRIMARY KEY (EMPNO),\n"+
"KEY DEPTNO (DEPTNO),\n"+
"CONSTRAINT emp_ibfk_5 FOREIGN KEY \n"+
"(DEPTNO)REFERENCES dept5 (DEPTNO)\n"+
")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
st.executeUpdate(table);
System.out.println("Created to table dept5 successfully");
*/
c.setAutoCommit(false);
// inserting data in the table
st.executeUpdate("INSERT INTO emp5 VALUES (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);");
st.executeUpdate("INSERT INTO emp5 VALUES (7698,'BLAKE','MANAGER',7839,'1981-01-05',2850,NULL,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);");
st.executeUpdate("INSERT INTO emp5 VALUES (7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);");
st.executeUpdate("INSERT INTO emp5 VALUES (7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7900,'JAMES','CLERK',7698,'1981-06-09',950,NULL,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);");
st.executeUpdate("INSERT INTO emp5 VALUES (7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);");
st.executeUpdate("INSERT INTO emp5 VALUES (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);");
st.executeUpdate("INSERT INTO emp5 VALUES (7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);");
st.executeUpdate("INSERT INTO emp5 VALUES (7876,'ADAMS','CLERK',7788,'1984-07-13',1100,NULL,20);");
st.executeUpdate("INSERT INTO emp5 VALUES (7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);");
//commit all insertion/transaction
c.commit();
// autocommit will treat above as a single transaction
c.setAutoCommit(true);
System.out.println("Data inserted in table emp5");
} catch (SQLException ex) { ex.getErrorCode();
String message = ex.getMessage();
System.out.println(message);
} finally {
System.out.println("going through final block");
try {
if (st== null || st.isClosed()) {
} else {
st.close();
}
if (c != null && !c.isClosed()) {
c.close(); }
} catch (SQLException ex) { ex.getErrorCode();
ex.getMessage();
}
}
System.out.println("dis-Connected to database successfully");
// st.close(); con.commit();con.close();
}
// main block ends here
}
|