Monday, 12 February 2018

JDBC

JDBC - 7 steps

1) import the package java.sql.*
2) a) Load the driver
   b) Register
3) Establish the connection
4) Create the statement
5) Excute the query
6) Process the result
7  Close


-------------------
1) Import packages
import java.sql.*;

main(){

2a) Load
2b)
     Class.forName("com.mysql.jdbcDriver");

3) Create a connection

  Connection con=DriverManager.getConnection("URL", "UN","pw");

4) Creating sql statements

  Statement st=con.createStatement();

5)  executing the query

   ResultSet rs = st.executeQuery("Select * from Students");


6)  while(rs.next()){
       
           sout(rs.getInt(1)+" "+rs.getString(2));

           }

7)   st.close();
     con.close();

}

-------------
DDL  -
DML  - updateQuery
DQL  - executeQuery


-------------------------------

import java.sql.*;

public class DemoClass {

public static void main(String args[]) throws Exception{

String url= "jdbc:mysql://localhost:3306/abc";
String uname="root";
String pass="1234";
String query= "insert into student values(4,'hari')";

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
//ResultSet rs=st.executeQuery(query);
    int count=st.executeUpdate(query);
    System.out.println("rows effected "+count);


st.close();
con.close();

}

}


-------------------------------------------

1) import packages

2) load driver
   Register driver

3) establish connectinon

4) creating statemns

5) executing queries

6) process

7) clsoe();


---------

import java.sql.*;

Class.forName(com.mysql.jdbc.Driver)

Connection con= DriverManager.getConnection()

Statement st=con.createStatement()

ResultSet rs= st.executeQuery()

while(rs.next())
{
   rs.getInt();
   rs.getString();
}

rs.close();
con.close();

No comments:

Post a Comment