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

Insert Into Database

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

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