Skip to main content

Posts

Showing posts with the label JDBC

how to store html form data in mysql database using java

how to store html form data in mysql database using java for explanation watch the video:: Step 1 : Create the peroject step 2 : create home.html in webapp step 3 : home.html home.html ========== <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script> <!-- Popper JS --> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script> <style> #div1 { width: 600p...

Transaction Management in JDBC | JDBC Tutorial

  Transaction Management in JDBC For explanation watch video :: Code:: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class OracleConn { public static void main(String[] args) { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String uname = "system"; String pwd = "tiger"; // take source account number and dest acc number from user Scanner scn = new Scanner(System.in); System.out.println("Enter source acc no: "); long srcAcNo = scn.nextLong(); System.out.println("Enter dest acc no: "); long destAcNo = scn.nextLong(); // take the amt to transfer System.out.println("Enter amt to transfer: "); long amt = scn.nextLong(); try (Connection con = DriverManager.getConnection(url, uname, pwd);  Statement st = con.createStatement();) { // Begin Tx if (con != null) con.set...

Batch Processing in JDBC

 Batch Processing in JDBC Code:: for explanation watch video : package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class OracleConn { public static void main(String[] args) { try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "tiger"); Statement st = con.createStatement()) { // add queries to batch ..these queries can belong to same db table or different // db tables but must be non-select Queries st.addBatch("INSERT INTO STUDENT VALUES(4444,'david',55.77,'france')"); st.addBatch("UPDATE  STUDENT SET AVG=AVG+10 WHERE SNO=2"); st.addBatch("DELETE FROM STUDENT WHERE SNO=1"); // execute the batch int result[] = st.executeBatch(); // find sum of the records that are effected int sum = 0; for (int i = 0; i < result.length; ++i)...

JDBC Auto Increment Primary Key in oracle

  JDBC Auto Increment Primary Key in oracle Create table SQL> create table stu50      ( sid number(10) primary key,        sname varchar2(15),         sadd  varchar2(15)      ); Create a sequence. SQL> create sequence sid_seq      start with 1 increment by 1; JDBC code:: ========= import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class OracleConn { private static final String query = "INSERT INTO STU50 VALUES(SID_SEQ.NEXTVAL,?,?)"; public static void main(String[] args) { try { //Connection Obj Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); PreparedStatement ps = con.prepareStatement(query); ps.setString(1, "smith"); ps.setString(2, "pune"); int count = ps.executeUpdate(); if(count!=0) ...

working with auto increment of mysql in JDBC

 working with auto increment of mysql in JDBC For explanation Watch video :: code:: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class AutoIncrementEx { private static final String query = "insert into emp2(ename,eadd) values(?,?)"; public static void main(String[] args) { try(Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); PreparedStatement ps = con.prepareStatement(query);){ ps.setString(1, "raja"); ps.setString(2, "nagpur"); int count = ps.executeUpdate(); if(count!=0) { System.out.println("Record Inserted"); }else { System.out.println("Record Not Inserted"); } }catch(SQLException se) { se.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } } }

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

Image Insertion into MySQL database Using java JDBC

 Image Insertion into MySQL database Using java JDBC  For explanation watch video: Code:: import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.Scanner; public class Test { private static final String query = "INSERT INTO IMG_TAB(CNAME,IMAGE) VALUES(?,?)"; public static void main(String[] args)throws Exception{ Scanner scn = new Scanner(System.in); System.out.println("Enter Customer name::"); String cname = scn.next(); System.out.println("Enter the image location: "); String loc = scn.next().replace("?", ""); InputStream is = new FileInputStream(loc); Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); PreparedStatement ps = con.prepareStatement(query); ps.setString(1, cname); ps.setBinaryStream(2, is); int count = ps.exe...

ResultSetMetaData in java

 ResultSetMetaData in java code:; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; class Test{ public static void main(String arg[]){ try(Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from stu");){ //create ResultSetMetaData obj ResultSetMetaData rsmd = rs.getMetaData(); //get column count int colCount = rsmd.getColumnCount();//3 //print column name for(int i=1;i<=colCount;i++) { System.out.print(rsmd.getColumnName(i)+" "); } System.out.println(); //for column type for(int i=1;i<=colCount;i++) { System.out.print(rsmd.getColumnTypeName(i)+" "); } System.out.println(); }catch(SQL...

JDBC Project - Log in Project With Mysql In Eclipse

JDBC Project - Log in Project With Mysql In Eclipse For explanation Watch Video :    Step 1: Create table 'login' in mysql step 2::  Insert Records Into 'login' table Step 3: Go to Eclipse and download swing related components Install Swing On Eclipse click on help at top of eclipse IDE after that click on install new software after that paste link in work with : 2020-12 - http://download.eclipse.org/releases/2020-12 i am using 2020-12 eclipse version at place of 2020-12 you can use your version name ex:: if your version of eclipse is oxygen then link : oxygen - http://download.eclipse.org/releases/oxygen expand click on general purpose tools and check the all tool from swing designer to end and after that click on  finish. After downloading all tools restart eclipse IDE. Download MySQL Jar File From here:  https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.25 for connection method:: Connection con = null; PreparedStatement ps = null; pu...

JDBC - execute method in jdbc for select and non-select queries

  JDBC - execute method in jdbc for select and non-select queries For Explanation Watch Video :: Creation of table:: SQL> create table test2(eid int,ename varchar2(10)); Table created. SQL> insert into test2 values(1,'vijay'); 1 row created. SQL> insert into test2 values(2,'rajesh'); 1 row created. SQL> select * from test2; EID ENAME ---------- ---------- 1 vijay 2 rajesh SQL> commit; Commit complete. JDBC code:: package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; class Test { public static void main(String arg[])throws Exception{ //generate the connection Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); //create statement Object Statement st = con.createStatement(); //get query from user Scanner ...

JDBC Program For Age Calculator

JDBC Program For Age Calculator For Explanation Watch video :: Code :  import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; class Test   {  private static final String query = " select (sysdate-dob)/365.25 from emp88 where eid = ?";     public static void main(String arg[]){     try(Scanner scn = new Scanner(System.in)){     //take eid from user     System.out.println("Enter Emp id : ");     int eid = scn.nextInt();     try(Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");     PreparedStatement ps = con.prepareStatement(query);){     //set the eid     ps.setInt(1, eid);     //ResultSet Object     ResultSet rs = ps.executeQuery();     r...

JDBC - Insert records into table using PreparedStatement

 JDBC - Insert records into table using PreparedStatement For Explanation watch Video : Code:: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Test { private static final String query = "insert into test1 values(?,?)"; public static void main(String[] args)throws Exception{ //generate connection try(Scanner scn = new Scanner(System.in)){ System.out.println("Enter The eid: "); int eid = scn.nextInt(); System.out.println("Enter employee name: "); String ename = scn.next(); try(Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); PreparedStatement ps = con.prepareStatement(query);){ ps.setInt(1, eid); ps.setString(2, ename); int count = ps.executeUpdate(); if(count==0) { System.o...

How to get the row count in JDBC?

How to get the row count in JDBC? For Explanation Watch Video:  Code:: package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Scanner; public class Test { public static void main(String[] args)throws Exception{ //generate connection Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); //sql Query String query = "select count(*) from stu"; //prparedStatement Object PreparedStatement ps = con.prepareStatement(query); //resultSet obj ResultSet rs = ps.executeQuery(); rs.next(); //fetch the data int count = rs.getInt(1); System.out.println("The No. Of Records : "+count); //close the connection con.close(); } }