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

Java&react Rough

Uploaded by

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

Java&react Rough

Uploaded by

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

JDBC LAB

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
class JDBCApp {
private static final String DB_URL = "jdbc:mysql://localhost:3306/ri_db";
private static final String DB_USER = "test";
private static final String DB_PASSWORD = "test123";
public static void createUser(Connection connection, String name, Double salary,
String country) throws
SQLException {
String sql = "INSERT INTO users (name, salary, country) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, name);
statement.setDouble(2, salary);
statement.setString(3, country);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {

System.out.println("A new user was inserted successfully!");


} else {
System.out.println("User insertion failed!");
}
} catch (SQLException e) {
System.out.println("Error in createUser: " + e.getMessage());
throw e;
}
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER,
DB_PASSWORD)) {
System.out.println("Enter details");
String name = scan.nextLine();
Double salary = scan.nextDouble();
String country = scan.nextLine();
createUser(connection, name, salary, country);

} catch (SQLException e) {
System.out.println("Error in main: " + e.getMessage());
e.printStackTrace();
}
}
}

Servlet

1. Create the HTML Form (index.html):


<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>

<body>
<h2>Login Form</h2>
<form action="login" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
2. Create the Servlet (LoginServlet.java):
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

// Hardcoded credentials for demonstration


private static final String VALID_NAME = "user";
private static final String VALID_PASSWORD = "password";

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
// Retrieve parameters from the request
String name = request.getParameter("name");
String password = request.getParameter("password");

// Check credentials
if (VALID_NAME.equals(name) && VALID_PASSWORD.equals(password)) {
response.getWriter().println("Welcome");
} else {
response.getWriter().println("Please check the username or password");
}
}
}
3. Configure the Servlet (web.xml):
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
app

// src/App.js
import React, { Component } from 'react';
import TaskForm from './components/TaskForm';
import TaskList from './components/TaskList';
import './App.css';

class App extends Component {


state = {
tasks: []
};

addTask = (task) => {


this.setState((prevState) => ({
tasks: [...prevState.tasks, { ...task, id: Date.now(), completed: false }]
}));
};

deleteTask = (id) => {


this.setState((prevState) => ({
tasks: prevState.tasks.filter(task => task.id !== id)
}));
};

toggleTaskCompletion = (id) => {


this.setState((prevState) => ({
tasks: prevState.tasks.map(task =>
task.id === id ? { ...task, completed: !task.completed } : task
)
}));
};

render() {
return (
<div className="App">
<h1>Task Tracker</h1>
<TaskForm addTask={this.addTask} />
<TaskList
tasks={this.state.tasks}
deleteTask={this.deleteTask}
toggleTaskCompletion={this.toggleTaskCompletion}
/>
</div>
);
}
}

export default App;


TF

TL

// src/components/TaskList.jsx
import React from 'react';
import TaskItem from './TaskItem';
import './TaskList.css';

const TaskList = ({ tasks, deleteTask, toggleTaskCompletion }) => {


return (
<div className="task-list">
{tasks.map(task => (
<TaskItem
key={task.id}
task={task}
deleteTask={deleteTask}
toggleTaskCompletion={toggleTaskCompletion}
/>
))}
</div>
);
};

export default TaskList;

TI

// src/components/TaskItem.jsx
import React from 'react';
import './TaskItem.css';

const TaskItem = ({ task, deleteTask, toggleTaskCompletion }) => {


return (
<div className={`task-item ${task.completed ? 'completed' : ''}`}>
<h3>{task.title}</h3>
<p>{task.description}</p>
<button onClick={() => toggleTaskCompletion(task.id)}>
{task.completed ? 'Mark Incomplete' : 'Mark Complete'}
</button>
<button onClick={() => deleteTask(task.id)}>Delete</button>
</div>
);
};

export default TaskItem;

You might also like