NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Sure, here's a simple example of how you can create a SQL database using Java and print the table for the employees' information from a CSV file:

Firstly, you'll need to have the CSV file (let's name it "employees.csv") with the structure you described: name, skill, contact, and email.

Next, you'll need the necessary libraries for handling CSV files (like OpenCSV) and JDBC for connecting to the SQL database. Make sure you have these dependencies in your project.

Here's an example code snippet:

```java
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;

public class CSVToSQL {
public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/employees_database";
String username = "your_username";
String password = "your_password";

try (Connection connection = DriverManager.getConnection(jdbcURL, username, password)) {
String createTableSQL = "CREATE TABLE IF NOT EXISTS employees (" +
"name VARCHAR(255)," +
"skill VARCHAR(255)," +
"contact VARCHAR(255)," +
"email VARCHAR(255)" +
")";

try (Statement statement = connection.createStatement()) {
statement.execute(createTableSQL);

try (CSVReader csvReader = new CSVReader(new FileReader("employees.csv"))) {
String[] record;
while ((record = csvReader.readNext()) != null) {
String name = record[0];
String skill = record[1];
String contact = record[2];
String email = record[3];

String insertSQL = "INSERT INTO employees (name, skill, contact, email) VALUES (?, ?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {
preparedStatement.setString(1, name);
preparedStatement.setString(2, skill);
preparedStatement.setString(3, contact);
preparedStatement.setString(4, email);
preparedStatement.executeUpdate();
}
}
}
}

// Print the table contents
String selectSQL = "SELECT * FROM employees";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSQL)) {
while (resultSet.next()) {
System.out.println(
resultSet.getString("name") + "t" +
resultSet.getString("skill") + "t" +
resultSet.getString("contact") + "t" +
resultSet.getString("email")
);
}
}
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
}
```

Replace "your_username" and "your_password" with your actual MySQL username and password respectively. Adjust the JDBC URL according to your database setup (for example, change "jdbc:mysql://localhost:3306/employees_database" to match your database URL).

This code creates a table "employees" in the database if it doesn't exist already, inserts the data from the CSV file, and then prints the table contents using `System.out.println`.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.