0% found this document useful (0 votes)
17 views3 pages

Ajp 19

The document contains code snippets from 3 Java programs that connect to MySQL databases and perform operations like retrieving data from tables, inserting multiple rows into a table, and querying data based on conditions. It includes the name and roll number of the student, Ashwin Pawar, and outputs from executing the different code snippets to connect and interact with MySQL databases from Java programs.

Uploaded by

gmpawar003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Ajp 19

The document contains code snippets from 3 Java programs that connect to MySQL databases and perform operations like retrieving data from tables, inserting multiple rows into a table, and querying data based on conditions. It includes the name and roll number of the student, Ashwin Pawar, and outputs from executing the different code snippets to connect and interact with MySQL databases from Java programs.

Uploaded by

gmpawar003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name :- Ashwin

pawar
Roll NO :23
Practical No. : 19

Program1:-
import java.sql.*;
class msbte_db {
public static void main(String args[]) {
try {
// Replace these with your MySQL database connection details
String url = "jdbc:mysql://localhost:3306/msbte_db";
String username = "root";
String password = "Shubh@mds258";
Connection cn = DriverManager.getConnection(url, username, password);
System.out.println("Connection to the database created");
Statement st = cn.createStatement();
String str = "select * from Student";
ResultSet rs = st.executeQuery(str);
String text = " ";
System.out.println("roll_no name");
while (rs.next()) {
text = text + rs.getInt(1) + "\t" + rs.getString(2) + "\n";
}
System.out.print(text);
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
Output:-
Program2:-

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class create_DB {
static final String JDBC_URL = "jdbc:mysql://localhost:3306/emp_db1";
static final String USER = "root";
static final String PASSWORD = "123456789";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(JDBC_URL,
USER, PASSWORD);
Statement statement = connection.createStatement()) {
// Inserting multiple rows
String insertDataSQL = "INSERT INTO employee (emp_id, emp_name)
VALUES "
+ "(101, 'Shivam'), "
+ "(102, 'Jay'), "
+ "(103, 'Viraj')";
statement.executeUpdate(insertDataSQL);
System.out.println("Multiple rows inserted successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:-

Program 3:-

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class stud_db1 {
public static void main(String[] args) {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/stud_db";
String username = "root";
String password = "123456789";
try (Connection connection = DriverManager.getConnection(url,
username, password)) {
// Query to select students with percentage > 70
String query = "SELECT Roll_no, Name FROM student WHERE Percentage >
70";
try (PreparedStatement preparedStatement =
connection.prepareStatement(query)) {
// Execute the query
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Display the results
while (resultSet.next()) {
int Roll_no = resultSet.getInt("Roll_no");
String Name = resultSet.getString("Name");
System.out.println("Roll_no: " + Roll_no + ", Name: " + Name);
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
Output:

You might also like