package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class JavaDB {
public static void main(String[] args) throws Exception {
createTable();
post();
get();
}
public static ArrayList<String> get() throws Exception{
try{
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement("select * from employee");
ResultSet rs = st.executeQuery();
ArrayList<String> array = new ArrayList<String>();
while(rs.next()) {
System.out.println(rs.getInt("id"));
System.out.print("");
System.out.println(rs.getString("name"));
array.add(rs.getString("name"));
}
return array;
}
catch(Exception e) {System.out.println(e);}
finally {System.out.println("query completed ");}
return null;
}
public static void createTable() throws Exception{
try {
System.out.println("in Create table");
Connection conn= getConnection();
System.out.println("created connection");
PreparedStatement st=conn.prepareStatement("create table IF NOT EXISTS employee(id INT AUTO_INCREMENT PRIMARY KEY,name varchar(20),age INT(10));");
st.executeUpdate();
}catch(Exception e) {
System.out.println(e);
}
finally {System.out.println("Function completed ...");}
}
public static void post() throws Exception{
//final int id= 99;
//final String name="srini";
//final int age=28;
try {
Connection conn= getConnection();
PreparedStatement st=conn.prepareStatement("insert into employee values(3,'sri',25);");
st.executeUpdate();
}catch(Exception e) {System.out.println(e);}
finally { System.out.println("insertion completed");
}
}
public static Connection getConnection() throws Exception{
try {
String driver ="com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/JavaDB";
String username="root";
String password="1234";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,username,password);
System.out.println("connected");
return conn;
}catch(Exception e){
System.out.println(e);
}
return null;
}
}
============
Nice Inforamtion Ruby on Rails Online Course Bangalore
ReplyDelete