0% found this document useful (0 votes)
9 views4 pages

JDBC Printing Table

Uploaded by

zxcrator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

JDBC Printing Table

Uploaded by

zxcrator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import com.mysql.cj.protocol.

Resultset;

import java.sql.*;

public class Main {


public static void main(String[] args) throws ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "FreeFire@12345";
String query = "select * from student;";

try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded succesfully");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,username,password);
System.out.println("connection establish succesfully ");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String course_name = rs.getString("course_name");
Double fees = rs.getDouble("fees");

System.out.println();
System.out.println("=============================");
System.out.println("ID : " + id);
System.out.println("NAME : " + name);
System.out.println("COURSE : " + course_name);
System.out.println("FEES : " + fees);
System.out.println("-----------------------------");

}
rs.close();
stmt.close();
con.close();
System.out.println("connection close succesfully ");
}catch (SQLException e){
e.printStackTrace();
}
}
}

================================================================================
INSERT

import com.mysql.cj.protocol.Resultset;
import java.sql.*;

public class Main {


public static void main(String[] args) throws ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "FreeFire@12345";
String query = "INSERT INTO student(id, name, course_name, fees) VALUES
(5,'Gaurav','SCIENCE',42500.0);";

try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded succesfully");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,username,password);
System.out.println("connection establish succesfully ");
Statement stmt = con.createStatement();
int rowsafected = stmt.executeUpdate(query);

if(rowsafected>0){
System.out.println("Insert Successfully !!");
}else{
System.out.println("Insertion failed !!" + rowsafected + " row(s)
affected ");
}

stmt.close();
con.close();
System.out.println("connection close succesfully ");
}catch (SQLException e){
e.printStackTrace();
}
}
}

================================================================================
DELETE

import com.mysql.cj.protocol.Resultset;

import java.sql.*;

public class Main {


public static void main(String[] args) throws ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "FreeFire@12345";
String query = "DELETE FROM student where id = 4 ;";

try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded succesfully");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,username,password);
System.out.println("connection establish succesfully ");
Statement stmt = con.createStatement();
int rowsafected = stmt.executeUpdate(query);

if(rowsafected>0){
System.out.println("Deletion Successfully !!");
}else{
System.out.println("Deletion failed !!" + rowsafected + " row(s)
affected ");
}

stmt.close();
con.close();
System.out.println("connection close succesfully ");
}catch (SQLException e){
e.printStackTrace();
}
}
}

================================================================================

import com.mysql.cj.protocol.Resultset;

import java.sql.*;

public class Main {


public static void main(String[] args) throws ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "FreeFire@12345";
String query = "UPDATE student"+" SET course_name = 'BBA' , fees = 4000.0"+"
WHERE id = 2;";

try{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver loaded succesfully");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,username,password);
System.out.println("connection establish succesfully ");
Statement stmt = con.createStatement();
int rowsafected = stmt.executeUpdate(query);

if(rowsafected>0){
System.out.println("UPDATE Successfully !!");
}else{
System.out.println("UPDATE failed !!" + rowsafected + " row(s)
affected ");
}

stmt.close();
con.close();
System.out.println("connection close succesfully ");
}catch (SQLException e){
e.printStackTrace();
}
}
}

================================================================================

You might also like