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]();