Commit 084f3540 authored by Suresh Kumar's avatar Suresh Kumar

JDBC

parent 6c965654
package jdbcassignment;
import java.sql.*;
public class FetchingRecords {
public static void main(String [] args) throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String url = "jdbc.mysql://localhost:3306/firstDBPractice";
String userName = "root";
String mysqlPassword = "nisum123";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstDBPractice","root","nisum123");
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from Employee");
while(resultSet.next()){
System.out.print(resultSet.getInt( "emp_id")+" "+resultSet.getString( "emp_name")+" "+resultSet.getString( "emp_address")
+ " " + resultSet.getString("emp_design") + " "+ resultSet.getString("emp_email") + " " + resultSet.getString("password"));
System.out.println();
}
} catch (Exception e){
System.out.println(e.getMessage());
}
}
}
package jdbcassignment;
import java.sql.*;
import java.util.Scanner;
public class RetrieveStudentDataFromConsole {
public static void main (String [] args) throws Exception{
Scanner sc = new Scanner(System.in);
Connection connection=null;
Statement statement =null;
ResultSet resultSet = null;
String url ="jdbc.mysql://localhost:3306/firstDBPractice";
String userName="root";
String mysqlPassword ="nisum123";
System.out.print("Enter Your Name Here .... ");
String name = sc.nextLine();
System.out.print("Enter Your Roll No ................");
String roll_no = sc.nextLine();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstDBPractice","root","nisum123");
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from Employee");
while(resultSet.next()){
if(resultSet.getString("emp_name").equals(name) || resultSet.getString("emp_id").equals(roll_no)){
System.out.println("Your Name is ----- Mr " + name + " And Your id Is ------ " + roll_no);
}
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
package jdbcassignment;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Scanner;
public class TakesRollNoFromConsol {
public static void main(String [] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Your Roll NO Here ---- > ");
String rollNo = sc.nextLine();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String url ="jdbc.mysql://localhost:3306/firstDBPractice";
String userName="root";
String nysqlPassword ="nisum123";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstDBPractice", "root", "nisum123");
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT student.student_id , student.student_name , course.course_id, course.course_name FROM student inner join course on course.course_id=student.course_id;");
while( resultSet.next()){
if (resultSet.getString("student_id").equals(rollNo)){
System.out.println(resultSet.getString("student_id") + " " + resultSet.getString("student_name") + " " + resultSet.getString("course_name") );
}
}
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment