Skip to content

Commit d3c50fa

Browse files
committed
java-7th folder updated 📈
1 parent d852b12 commit d3c50fa

6 files changed

+109
-0
lines changed

java-7th/ConnectDb.class

1.91 KB
Binary file not shown.

java-7th/ConnectDb.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.sql.*;// importing the sql package
2+
3+
class ConnectDb{
4+
public static void main(String [] args){
5+
try{
6+
// register the Driver Class
7+
Class.forName("com.mysql.cj.jdbc.Driver");
8+
//connect to the database
9+
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hackathon", "root", "len0v0@33214");
10+
11+
// create Statement
12+
Statement st = con.createStatement();
13+
14+
// Query
15+
16+
String query = "SELECT * FROM participants";
17+
18+
// executing the query
19+
20+
ResultSet rs = st.executeQuery(query);
21+
22+
while(rs.next()){
23+
System.out.println("ID:- "+ rs.getInt(1) + "\nName:- " + rs.getString(2)+"\nSkills:- " + rs.getString(3));
24+
25+
}
26+
27+
con.close();
28+
}catch(ClassNotFoundException e){
29+
System.out.println(e.getMessage());
30+
}catch(SQLException e){
31+
System.out.println(e.getMessage());
32+
}
33+
}
34+
}

java-7th/StudentDb.class

1.93 KB
Binary file not shown.

java-7th/StudentDb.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.sql.*;
2+
3+
4+
public class StudentDb {
5+
6+
public static void main(String [] args){
7+
8+
try{
9+
// Register the Driver class
10+
Class.forName("com.mysql.cj.jdbc.Driver");
11+
12+
// Connect to the Database
13+
14+
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ncit", "root", "len0v0@33214");
15+
16+
// create Statement
17+
18+
Statement st = con.createStatement();
19+
20+
String query = "SELECT * FROM students WHERE faculty = 'SE'";
21+
22+
ResultSet rs = st.executeQuery(query);
23+
24+
while(rs.next()){
25+
26+
System.out.println("Roll Number:- " + rs.getInt(1)+ "\nName:- "+ rs.getString(2) + "\nFaculty:- " + rs.getString(3));
27+
28+
}
29+
30+
con.close();
31+
32+
}catch(ClassNotFoundException e)
33+
{
34+
System.out.println(e.getMessage());
35+
}catch(SQLException e){
36+
System.out.println(e.getMessage());
37+
}
38+
39+
}
40+
}
2.4 MB
Binary file not shown.

java-7th/readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
**Steps to connect Java application to the database :sagittarius:**
2+
3+
step 1:- Register the Driver class
4+
5+
step 2:- Connect to the Database
6+
7+
step 3:- Prepare/create Statement
8+
9+
step 4:- Execute Query
10+
11+
step 5: Close the connection
12+
13+
**How to compile and run the program? ❓**
14+
15+
step 1: import the .jar file into your workspace
16+
17+
step2: compile the Java program as `javac Example.java`
18+
19+
step3: provide the complete jar file path and run the program as
20+
21+
java -p complete_path_of_jar_file.jar JavaFileName
22+
23+
**Note 🗒️** :- There are various other methods of setting the path of jar file except this, so you set the path according
24+
25+
to your choice...
26+
27+
28+
# `LABA-7 Programming Question 📖`
29+
*Qn no. 1:- Write a Java Program to show all the data of the table*
30+
31+
Answer:- *ConnectDb.java*
32+
33+
**Qn no. 2:- Write a Java Program to print only those records whose faculty is SE given table name Students**
34+
35+
Answer:- *StudentJDb.java*

0 commit comments

Comments
 (0)