Created
May 21, 2019 07:01
-
-
Save avinashseth/7b8cbecdefe374b6b15390a0ac32d9be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package drinking; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class Dblibrary { | |
private Connection connection; | |
private Statement statement; | |
private ResultSet result; | |
public Dblibrary() { | |
try { | |
Class.forName("oracle.jdbc.driver.OracleDriver"); | |
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "avinash", "123456"); | |
statement = connection.createStatement(); | |
result = null; | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("Connection not found"); | |
} | |
} | |
public void readTheData() { | |
try { | |
statement = connection.createStatement(); | |
result = statement.executeQuery("SELECT * FROM interns ORDER BY id"); | |
while(result.next()) { | |
System.out.println("Intern id:" + result.getInt(1) + " | Intern name:" + result.getString(2) + " | Intern Points:" + result.getInt(3)); | |
} | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("Unable to talk to database"); | |
} | |
} | |
public void createData(String name, int points) { | |
try { | |
result = statement.executeQuery("INSERT INTO interns (intern_name, points) VALUES ('" + name + "', " + points + ")"); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("Unable to insert the data"); | |
} | |
} | |
public boolean updateData(int internId, String name, int points) { | |
// which table to be updated table interns | |
// what fields to be updated intern_name, points | |
// which row has to be update id | |
try { | |
int returnRowCount = statement.executeUpdate("UPDATE interns SET intern_name = '" + name + "', points = " + points + " WHERE id = " + internId); | |
if(returnRowCount == 1) { | |
return true; | |
} else { | |
return false; | |
} | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("Unable to insert the data" + e); | |
} | |
return false; | |
} | |
public int deleteRow(int internId) { | |
int returnRowCount = 0; | |
try { | |
returnRowCount = statement.executeUpdate("DELETE FROM interns WHERE id = " + internId); | |
return returnRowCount; | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("Unable to insert the data" + e); | |
} | |
return returnRowCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment