0% found this document useful (0 votes)
2 views

Database and Networking

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Database and Networking

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program: Sample Databse Connectivity:

import java.sql.*;

public class DatabaseConnection {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/sampledb";

String user = "root";

String password = "vaishnavi@2024";

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Connection conn = DriverManager.getConnection(url, user, password);

System.out.println("Connected to sampledb successfully!");

conn.close();

} catch (Exception e) {

System.out.println("Connection failed!");

e.printStackTrace();

Program: Creation of Table, Inserting Values, Updating Rows (Add everything in try block after connection)

Statement stmt = conn.createStatement();

String usedb = "use sample";


stmt.executeUpdate(usedb);

String createtb = "Create table data(Roll int, Name varchar(255))";


stmt.executeUpdate(createtb);

String inserttb = "insert into data (Roll,Name)values(12,'vaishnavi')";


stmt.executeUpdate(inserttb);

String deleteqr=”delete from data where Roll=12”;


stmt.executeUpdate(deleteqr);

String updateqr=”Update data SET Name =’ Vaishu’ where Roll=12”;


stmt.executeUpdate(updateqr);

String insertQuery = "INSERT INTO data (Roll, Name) VALUES (" + rollInsert + ", '" + nameInsert + "')";
stmt.executeUpdate(insertQuery); // user input
Program: Deleting rows, Inserting Values, Updating Rows using Prepared Statements for user input

String query = "INSERT INTO data (Roll, Name) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 16);
pstmt.setString(2, "dhuttarge");
pstmt.executeUpdate();

String updateQuery = "UPDATE data SET Name = ? WHERE Roll = ?";


PreparedStatement updateStmt = conn.prepareStatement(updateQuery);
updateStmt.setString(1, nameUpdate);
updateStmt.setInt(2, rollUpdate);
updateStmt.executeUpdate();

String deleteQuery = "DELETE FROM data WHERE Roll = ?";


PreparedStatement deleteStmt = conn.prepareStatement(deleteQuery);
deleteStmt.setInt(1, rollDelete);
deleteStmt.executeUpdate();

Without taking user input


String insertQuery = "INSERT INTO data (Roll, Name) VALUES (?, ?)";
PreparedStatement pstmtInsert = conn.prepareStatement(insertQuery);
pstmtInsert.setInt(1, 12);
pstmtInsert.setString(2, "vaishnavi"); // Use double quotes for strings
pstmtInsert.executeUpdate();

Program: To retrieve data from table

Statement stmt = con.createStatement();

String query = "SELECT * FROM data";

ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
int roll = rs.getInt("Roll");
String name = rs.getString("Name");
System.out.println("Roll: " + roll + ", Name: " + name);
}
Retriving hostname and IP address using InetAddress

InetAddress host = InetAddress.getLocalHost();


host.getHostName();
host.getHostAddress();

Retriving IP address using hostname


System.out.println("IP Address: " + InetAddress.getByName(host).getHostAddress());

Various nethods of URL class and URL connection

URL url = new URL("https://www.google.com");


URLConnection urlConnection = url.openConnection();

You might also like