diff --git a/telusko/jdbc/.gitignore b/telusko/jdbc/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/telusko/jdbc/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/telusko/jdbc/.idea/.gitignore b/telusko/jdbc/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/telusko/jdbc/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/telusko/jdbc/.idea/encodings.xml b/telusko/jdbc/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/telusko/jdbc/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/telusko/jdbc/.idea/misc.xml b/telusko/jdbc/.idea/misc.xml new file mode 100644 index 0000000..fdc35ea --- /dev/null +++ b/telusko/jdbc/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/telusko/jdbc/.idea/vcs.xml b/telusko/jdbc/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/telusko/jdbc/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/telusko/jdbc/pom.xml b/telusko/jdbc/pom.xml new file mode 100644 index 0000000..ad00403 --- /dev/null +++ b/telusko/jdbc/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.akash.java + jdbc + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + com.mysql + mysql-connector-j + 9.0.0 + + + + \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main.java b/telusko/jdbc/src/main/java/org/akash/java/Main.java new file mode 100644 index 0000000..6205de2 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main.java @@ -0,0 +1,36 @@ +package org.akash.java; +import java.sql.*; + +public class Main { + public static void main(String[] args) throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + Connection connection = null; + Statement statement = null; + try { + connection = DriverManager.getConnection(url,user,password); + System.out.println(connection.getClientInfo()); + + statement = connection.createStatement(); +// String insertQuery = "INSERT into studentInfo (id, sname, sage, scity) values(1, 'Akash', 25, 'Karimnagar')"; + String insertQuery = "INSERT into studentInfo (id, sname, sage, scity) values(2, 'Anil', 23, 'Hyderabad')"; + + int noOfRowsEffect = statement.executeUpdate(insertQuery); + if(noOfRowsEffect == 0){ + System.out.println("Something is wrong"); + }else { + System.out.println("No of Rows effected: "+ noOfRowsEffect); + } + }catch (Exception e){ + System.out.println(e.getMessage()); + + }finally { + assert statement != null; + statement.close(); + connection.close(); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main2.java b/telusko/jdbc/src/main/java/org/akash/java/Main2.java new file mode 100644 index 0000000..54c7d60 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main2.java @@ -0,0 +1,40 @@ +package org.akash.java; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Main2 { + public static void main(String[] args) throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + Connection connection = null; + Statement statement = null; + try { + connection = DriverManager.getConnection(url,user,password); + System.out.println(connection.getClientInfo()); + + statement = connection.createStatement(); + + String updateQuery = "UPDATE studentInfo set sage=40 where id=2"; + + int noOfRowsEffect = statement.executeUpdate(updateQuery); + if(noOfRowsEffect == 0){ + System.out.println("Something is wrong"); + }else { + System.out.println("No of Rows effected: "+ noOfRowsEffect); + } + }catch (Exception e){ + System.out.println(e.getMessage()); + + }finally { + assert statement != null; + statement.close(); + connection.close(); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main3.java b/telusko/jdbc/src/main/java/org/akash/java/Main3.java new file mode 100644 index 0000000..8d75d28 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main3.java @@ -0,0 +1,39 @@ +package org.akash.java; + +import java.sql.*; + +public class Main3 { + public static void main(String[] args) throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + Connection connection = null; + Statement statement = null; + try { + connection = DriverManager.getConnection(url,user,password); + System.out.println(connection.getClientInfo()); + + statement = connection.createStatement(); + String fetchQuery = "SELECT * from studentInfo"; + + ResultSet set = statement.executeQuery(fetchQuery); + while (set.next()){ + int id = set.getInt(1); + String name = set.getString(2); + int age = set.getInt(3); + + System.out.println(name + " "+ id+ " "+ age); + } + + }catch (Exception e){ + System.out.println(e.getMessage()); + + }finally { + assert statement != null; + statement.close(); + connection.close(); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main4.java b/telusko/jdbc/src/main/java/org/akash/java/Main4.java new file mode 100644 index 0000000..7e4d6de --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main4.java @@ -0,0 +1,36 @@ +package org.akash.java; + +import java.sql.*; + +public class Main4 { + public static void main(String[] args) throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + Connection connection = null; + Statement statement = null; + try { + connection = DriverManager.getConnection(url,user,password); + System.out.println(connection.getClientInfo()); + + statement = connection.createStatement(); + String deleteQuery = "DELETE from studentInfo where id=2"; + + int noOfRowsEffect = statement.executeUpdate(deleteQuery); + if(noOfRowsEffect == 0){ + System.out.println("Something is wrong"); + }else { + System.out.println("No of Rows effected: "+ noOfRowsEffect); + } + }catch (Exception e){ + System.out.println(e.getMessage()); + + }finally { + assert statement != null; + statement.close(); + connection.close(); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main5.java b/telusko/jdbc/src/main/java/org/akash/java/Main5.java new file mode 100644 index 0000000..621bee3 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main5.java @@ -0,0 +1,47 @@ +package org.akash.java; + +import java.sql.*; + +public class Main5 { + public static void main(String[] args) throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + Connection connection = null; + Statement statement = null; + try { + connection = DriverManager.getConnection(url,user,password); + System.out.println(connection.getClientInfo()); + + statement = connection.createStatement(); + String insertQuery = "INSERT into studentInfo (id, sname, sage, scity) values(2, 'Anil', 23, 'Hyderabad')"; + String fetchQuery = "SELECT * from studentInfo"; + String deleteQuery = "DELETE from studentInfo where id=2"; + + boolean status = statement.execute(deleteQuery); + + if(status){ + ResultSet set = statement.getResultSet(); + while (set.next()){ + int id = set.getInt(1); + String name = set.getString(2); + int age = set.getInt(3); + + System.out.println(name + " "+ id+ " "+ age); + } + } else { + int rows = statement.getUpdateCount(); + if(rows<=0)System. out. println("Operation failed! " ) ; + else System.out.println("Operation successful!" ) ; + } + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + assert statement != null; + statement.close(); + connection.close(); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main6.java b/telusko/jdbc/src/main/java/org/akash/java/Main6.java new file mode 100644 index 0000000..d0d4f96 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main6.java @@ -0,0 +1,44 @@ +package org.akash.java; + +import org.akash.java.util.JDBC; + +import java.sql.*; + +public class Main6 { + public static void main(String[] args) throws SQLException { + Connection connection = null; + Statement statement = null; + try { + connection = JDBC.getConnection(); + + statement = connection.createStatement(); + String insertQuery = "INSERT into studentInfo (id, sname, sage, scity) values(2, 'Anil', 23, 'Hyderabad')"; + String fetchQuery = "SELECT * from studentInfo"; + String deleteQuery = "DELETE from studentInfo where id=2"; + + boolean status = statement.execute(deleteQuery); + + if(status){ + ResultSet set = statement.getResultSet(); + while (set.next()){ + int id = set.getInt(1); + String name = set.getString(2); + int age = set.getInt(3); + + System.out.println(name + " "+ id+ " "+ age); + } + } else { + int rows = statement.getUpdateCount(); + if(rows<=0)System. out. println("Operation failed! " ) ; + else System.out.println("Operation successful!" ) ; + } + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + assert statement != null; + JDBC.closeConnection(connection, statement); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main7.java b/telusko/jdbc/src/main/java/org/akash/java/Main7.java new file mode 100644 index 0000000..bb99b96 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main7.java @@ -0,0 +1,56 @@ +package org.akash.java; + +import org.akash.java.util.JDBC; + +import java.sql.*; +import java.util.Scanner; + +public class Main7 { + public static void main(String[] args) throws SQLException { + Connection connection = null; + Scanner sc = new Scanner(System.in); + PreparedStatement preparedStatement = null; + try { + connection = JDBC.getConnection(); + String insertQuery = "INSERT into studentInfo (id, sname, sage, scity) values(?,?,?,?)"; + + preparedStatement = connection.prepareStatement(insertQuery); + + int id = sc.nextInt(); + String name = sc.next(); + int age = sc.nextInt(); + String city = sc.next(); + + preparedStatement.setInt(1, id); + preparedStatement.setString(2, name); + preparedStatement.setInt(3, age); + preparedStatement.setString(4, city); + + + + boolean status = preparedStatement.execute(); + + if(status){ + ResultSet set = preparedStatement.getResultSet(); + while (set.next()){ + id = set.getInt(1); + name = set.getString(2); + age = set.getInt(3); + + System.out.println(name + " "+ id+ " "+ age); + } + } else { + int rows = preparedStatement.getUpdateCount(); + if(rows<=0)System. out. println("Operation failed! " ) ; + else System.out.println("Operation successful!" ) ; + } + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + assert preparedStatement != null; + JDBC.closeConnection(connection, preparedStatement); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main8.java b/telusko/jdbc/src/main/java/org/akash/java/Main8.java new file mode 100644 index 0000000..c64e3a2 --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main8.java @@ -0,0 +1,54 @@ +package org.akash.java; + +import org.akash.java.util.JDBC; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class Main8 { + public static void main(String[] args) throws SQLException { + Connection connection = null; + Scanner sc = new Scanner(System.in); + PreparedStatement preparedStatement = null; + try { + connection = JDBC.getConnection(); + String updateQuery = "UPDATE studentInfo set sage=? where id=?"; + + preparedStatement = connection.prepareStatement(updateQuery); + + int id = sc.nextInt(); + int age = sc.nextInt(); + + preparedStatement.setInt(2, id); + preparedStatement.setInt(1, age); + + + + boolean status = preparedStatement.execute(); + + if(status){ + ResultSet set = preparedStatement.getResultSet(); + while (set.next()){ + id = set.getInt(1); + age = set.getInt(3); + + System.out.println(id+ " "+ age); + } + } else { + int rows = preparedStatement.getUpdateCount(); + if(rows<=0)System. out. println("Operation failed! " ) ; + else System.out.println("Operation successful!" ) ; + } + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + assert preparedStatement != null; + JDBC.closeConnection(connection, preparedStatement); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/Main9.java b/telusko/jdbc/src/main/java/org/akash/java/Main9.java new file mode 100644 index 0000000..107b71e --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/Main9.java @@ -0,0 +1,45 @@ +package org.akash.java; + +import org.akash.java.util.JDBC; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class Main9 { + public static void main(String[] args) throws SQLException { + Connection connection = null; + Scanner sc = new Scanner(System.in); + PreparedStatement preparedStatement = null; + try { + connection = JDBC.getConnection(); + String updateQuery = "UPDATE studentInfo set sage=? where id=?"; + + preparedStatement = connection.prepareStatement(updateQuery); + + + preparedStatement.setInt(2, 1); + preparedStatement.setInt(1, 28); + preparedStatement.addBatch(); + + preparedStatement.setInt(2, 2); + preparedStatement.setInt(1, 24); + preparedStatement.addBatch(); + + preparedStatement.execute(); + + int rows = preparedStatement.executeUpdate(); + if(rows<=0)System. out. println("Operation failed! " ) ; + else System.out.println("Operation successful!" ) ; + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + assert preparedStatement != null; + JDBC.closeConnection(connection, preparedStatement); + } + + + } +} \ No newline at end of file diff --git a/telusko/jdbc/src/main/java/org/akash/java/util/JDBC.java b/telusko/jdbc/src/main/java/org/akash/java/util/JDBC.java new file mode 100644 index 0000000..95be51a --- /dev/null +++ b/telusko/jdbc/src/main/java/org/akash/java/util/JDBC.java @@ -0,0 +1,18 @@ +package org.akash.java.util; +import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; + +public class JDBC { + Connection connection; + public static Connection getConnection() throws SQLException { + String url = "jdbc:mysql://localhost:3306/jdbc_learning"; + String user = "root"; + String password = "root"; + return DriverManager.getConnection(url,user,password); + } + public static void closeConnection(Connection connection, Statement statement) throws SQLException { + statement.close(); + connection.close(); + } +} diff --git a/telusko/jdbc/src/main/resources/initdb.sql b/telusko/jdbc/src/main/resources/initdb.sql new file mode 100644 index 0000000..ab56061 --- /dev/null +++ b/telusko/jdbc/src/main/resources/initdb.sql @@ -0,0 +1,11 @@ +create database if not exists jdbc_learning; + +use jdbc_learning; + +create table if not exists studentInfo( +id int not null, +sname varchar(40) not null, +sage int not null, +scity varchar(20) not null, +primary key(id) +) \ No newline at end of file