0% found this document useful (0 votes)
8 views2 pages

JDBC Connection

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)
8 views2 pages

JDBC Connection

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/ 2

Connecting to JDBC to MySQL

1.Open Eclipse and Create Maven Project – “MySQLJavaConnection”.

2.In MySQLJavaConnection open pom.xml file and write the following code

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MySQLJavaConnection</groupId>
<artifactId>MySQLJavaConnection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</project>

3. Now create java class name with MySQLJavaConnection and write down following code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLJavaConnection {

public static void main(String[] args) throws SQLException{


Connection conn=null;
try
{
String url="jdbc:mysql://localhost:3307/mysqldemo";
String user="root";
String password="Root@123";
conn=DriverManager.getConnection(url,user,password);
if(conn!=null)
{
System.out.println("Connected to the database mysqldemo");

}
//create

String sql="create table Employee("


+ "EmployeeID int,"
+ "Name varchar(20),"
+ "Address varchar(20),"
+ "City varchar(10)"
+ ");";
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("Created table in given database");

//insert
String sql="insert into Employee(EmployeeID,Name,Address,City)"
+ "values (?,?,?,?);";
PreparedStatement statement=conn.prepareStatement(sql);
statement.setInt(1,101);
statement.setString(2,"John");
statement.setString(3,"Hyderabad");
statement.setString(4,"India");
int rowsInserted=statement.executeUpdate();
if(rowsInserted >0)
{
System.out.println("A new row was inserted successfully!");
}

//Update
String sql="Update Employee set city=? where EmployeeID=?";
PreparedStatement statement=conn.prepareStatement(sql);
statement.setString(1,"Hyderabad");
statement.setInt(2,101);
int rowsUpdated=statement.executeUpdate();
if(rowsUpdated >0)
{
System.out.println("A new row was Updated successfully!");
}
//Delete
String sql="delete from Employee where EmployeeID=?";
PreparedStatement statement=conn.prepareStatement(sql);
statement.setInt(1,101);
int rowsDeleted=statement.executeUpdate();
if(rowsDeleted>0)
{
System.out.println("A row was deleted successfully!");
}

}
catch(SQLException ex)
{
System.out.println("Exception ::" +ex.getMessage());
ex.printStackTrace();
}
finally
{
conn.close();
}

}
}

You might also like