React Native Developer

Monday, December 24, 2018

JDBC CONNECTIVITY IN JAVA


  •  MysqlCon.file


import java.sql.*;

class MysqlCon{

public static void main(String args[]){

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

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javadb","root","");
Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

}
}

  • JDBCINSETDEMO.JAVA

import java.sql.*;
import java.util.Scanner;

public class JdbcInsertDemo
{
public static void main(String[] args) throws Exception
{
String dbURL = "jdbc:mysql://localhost:3306/javadb";
String username = "root";
String password = "";
try
{
Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(dbURL, username, password);
String sql = "INSERT INTO Users (username, password, fullname, email) VALUES (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
Scanner input = new Scanner(System.in);
System.out.println("Enter the Username");
String uname = input.next();
System.out.println("Enter the Password");
String pwd = input.next();
System.out.println("Enter the FullName");
String fname = input.next();
System.out.println("Enter the Email ");
String email = input.next();
statement.setString(1, uname);
statement.setString(2, pwd);
statement.setString(3, fname);
statement.setString(4, email);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0)
{
System.out.println("A new user was inserted successfully!");
}
}
      catch (SQLException ex)
{
   ex.printStackTrace();
}
}
}


  • JDBCUPDATEDEMO.JAVA
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
/**
 * A sample program that demonstrates how to execute SQL UPDATE statement
 * using JDBC.
 * @author www.codejava.net
 *
 */
public class JdbcUpdateDemo {

public static void main(String[] args) throws Exception
 {
String dbURL = "jdbc:mysql://localhost:3306/javadb";
String username = "root";
String password = "";

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

Connection conn = DriverManager.getConnection(dbURL, username, password);
String sql = "UPDATE Users SET password=?, fullname=?, email=? WHERE username=?";
System.out.println("Enter usename you have to update=");
Scanner input = new Scanner(System.in);
String uname = input.next();
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, uname);
statement.setString(2, "William");
statement.setString(3, "bill@gates");
statement.setString(4, "bill");

int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("An existing user was updated successfully!");
}


} catch (SQLException ex) {
ex.printStackTrace();
}
}
}


  • JDBCDELETEDEMO.JAVA
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
/**
 * A sample program that demonstrates how to execute SQL DELETE statement
 * using JDBC. 
 * @author www.codejava.net
 *
 */
public class JdbcDeleteDemo {

public static void main(String[] args) throws Exception
{
String dbURL = "jdbc:mysql://localhost:3306/javadb";
String username = "root";
String password = "";
try  {
Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(dbURL, username, password)
String sql = "DELETE FROM Users WHERE username=?";
Scanner input = new Scanner(System.in);
System.out.println("Enter the Username");
String uname = input.next();
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, uname);
int rowsDeleted = statement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("A user was deleted successfully!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

  • JDBCSELECTDEMO.JAVA
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * A sample program that demonstrates how to execute SQL SELECT statement
 * using JDBC. 
 * @author www.codejava.net
 *
 */
public class JdbcSelectDemo {

public static void main(String[] args) throws Exception
 {
String dbURL = "jdbc:mysql://localhost:3306/javadb";
String username = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(dbURL, username, password);
String sql = "SELECT * FROM Users";
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
int count = 0;
while (result.next()){
String name = result.getString(2);
String pass = result.getString(3);
String fullname = result.getString("fullname");
String email = result.getString("email");
String output = "User #%d: %s - %s - %s - %s";
System.out.println(String.format(output, ++count, name, pass, fullname, email));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}


No comments:

Post a Comment