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

Insert Into Database

This document contains code to insert data into a database table and retrieve data from a database table. The insert code uses a PreparedStatement to add a new record to the "users" table with an id of 3, first name of "tatenda", and surname of "rumvura". The retrieve code uses a Statement to execute a query selecting all records from the "users" table, retrieves the first name and surname columns, and prints them to the console.

Uploaded by

lastmadanhire
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)
79 views2 pages

Insert Into Database

This document contains code to insert data into a database table and retrieve data from a database table. The insert code uses a PreparedStatement to add a new record to the "users" table with an id of 3, first name of "tatenda", and surname of "rumvura". The retrieve code uses a Statement to execute a query selecting all records from the "users" table, retrieves the first name and surname columns, and prints them to the console.

Uploaded by

lastmadanhire
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

Insert into database

package javapractice;
import java.sql.*;
public class Javapractice {
public static void main(String[] args) {
try
{
Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/javadb","root","root");
//ResultSet rs =st.executeQuery("SELECT * FROM users");
PreparedStatement st =con.prepareStatement("INSERT INTO users
(id,firstname,surname)values(3,'tatenda','rumvura')");
int a =st.executeUpdate();
if (a>0){
System.out.println("row updated");
}
con.close();
}
catch(Exception e)
{

}
}

}
Retrieve data

package javapractice;
import java.sql.*;
public class Javapractice {
public static void main(String[] args) {
try
{
Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/javadb","root","root");
Statement st =con.createStatement();
ResultSet rs =st.executeQuery("SELECT * FROM users");
rs.next();
String firstname=rs.getString(2);
String surname=rs.getString(3);
System.out.println(firstname +" "+surname);
con.close();
}
catch(Exception e)
{

}
}

You might also like