CREATE TABLE `emp` ( `eno` bigint(20) UNSIGNED NOT NULL, `ename` varchar(30) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `emp` (`eno`, `ename`) VALUES (1, 'Dev'), (2, 'Darshee'), (3, 'Krishna'), (4, 'Bhavya');
package class1; import java.sql.*; /** * * @author Dhaval Mehta */ public class mysample1 { /** * @param args the command line arguments */ public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); } catch (Exception ex) { System.out.print("Driver Load Problem : "+ ex.getMessage()); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost/dm?" + "user=dm&password=dm12"); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM emp"); while(rs.next()) { System.out.print(""+rs.getInt("eno")); System.out.print("-"+rs.getString("ename")); } } catch(Exception e) { System.out.print("Problem : "+ e.getMessage()); } } }
package class1; import java.sql.*; import java.util.*; /** * * @author Dhaval Mehta */ public class mysampleinsert { /** * @param args the command line arguments */ public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); } catch (Exception ex) { System.out.print("Driver Load Problem : "+ ex.getMessage()); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost/dm?" + "user=dm&password=dm12"); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } try { Scanner sc= new Scanner(System.in); System.out.print("Enter Your name "); String ename= sc.next(); stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO `emp` (`eno`, `ename`) VALUES (NULL, '"+ ename +"');"); System.out.print(ename + " is added successfully"); } catch(Exception e) { System.out.println("Error : " + e.getMessage()); } } }