0% found this document useful (0 votes)
6 views4 pages

Java Database Operations Examples

The document contains Java code examples for retrieving and inserting items into a MySQL database. It demonstrates how to connect to a database, execute SQL queries, and handle exceptions. Two separate examples for both retrieving and inserting data are provided for different tables in two databases: 'cs' and 'kisumu'.

Uploaded by

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

Java Database Operations Examples

The document contains Java code examples for retrieving and inserting items into a MySQL database. It demonstrates how to connect to a database, execute SQL queries, and handle exceptions. Two separate examples for both retrieving and inserting data are provided for different tables in two databases: 'cs' and 'kisumu'.

Uploaded by

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

EXAMPLE 1

Retrieving items from a database

import [Link].*;
public class MMM {
public static void main(String[] args) {
String url="jdbc:mysql://localhost:3306/cs";
String username="root";
String password="";
try{
[Link]("[Link]");
try (Connection con = [Link](url,username,password)) {
Statement statement=[Link]();
ResultSet resultSet=[Link]("select * from stude");
while([Link]())
{

String fname = [Link]("fnam");


String sname = [Link]("snam");
[Link]( fname
+ "\t\t" + sname);
}
}
}
catch(Exception e){
[Link](e);

}
}

EXAMPLE 2

import [Link].*;
public class City {
public static void main(String[] args) {
String url="jdbc:mysql://localhost:3306/kisumu";
String username="root";
String password="";
try{
[Link]("[Link]");
try (Connection con = [Link](url,username,password)) {
Statement statement=[Link]();
ResultSet resultSet=[Link]("select * from login");
while([Link]())
{

String Cities = [Link]("Cities");

[Link]( Cities);

}
}
}
catch(Exception e){
[Link](e);

}
Inserting Items to a database

Example 1

import [Link];
import [Link].*;
import [Link];
import [Link];

import [Link];
public class IntoDB2 {

static String URL = "jdbc:mysql://localhost:3306/cs";

public static void main(String[] args) {


try {
[Link]("[Link]");
Connection conn =[Link](URL,"root","");
Statement stmt = [Link]();
String sql;
sql = "INSERT INTO stude VALUES ('calvins','otieno')";
[Link](sql);
[Link]("Inserted records into the table...");
} catch(Exception e) {
[Link]();

}
}

Example 2
import [Link];
import [Link].*;
import [Link];
import [Link];

import [Link];
public class Otoyo {

static String URL = "jdbc:mysql://localhost:3306/kisumu";

public static void main(String[] args) {


try {
[Link]("[Link]");
Connection conn =[Link](URL,"root","");
Statement stmt = [Link]();
String sql;
sql = "INSERT INTO login VALUES ('onyango')";
[Link](sql);
[Link]("Inserted records into the table...");
} catch(Exception e) {
[Link]();

}
Example 3
import [Link];
import [Link].*;
import [Link];
import [Link];

import [Link];
public class Min {

static String URL = "jdbc:mysql://localhost:3306/kisumu";


public static void main(String[] args) {
try {
[Link]("[Link]");
Connection conn =[Link](URL,"root","");
Statement stmt = [Link]();
String sql;
sql = "INSERT INTO login VALUES ('onyango')";
[Link](sql);
sql = "INSERT INTO login VALUES ('owino')";
[Link](sql);
sql = "INSERT INTO login VALUES ('obambo')";
[Link](sql);
[Link]("Inserted records into the table...");
} catch(Exception e) {
[Link]();

You might also like