Skip to content

Instantly share code, notes, and snippets.

@adilkurniaramdan
Created February 14, 2015 16:24
Show Gist options
  • Save adilkurniaramdan/5e2854e3ff542dcb0d94 to your computer and use it in GitHub Desktop.
Save adilkurniaramdan/5e2854e3ff542dcb0d94 to your computer and use it in GitHub Desktop.
File OdbcConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author adildramdan
*/
public class OdbcConnection {
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url = "jdbc:odbc:namadatabase";
private static final String username = "";
private static final String password = "";
private static Connection conn;
public static Connection getConnection() throws Exception {
if (conn == null) {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
}
return conn;
}
}
File TestObject.java
/**
*
* @author adildramdan
*/
public class TestObject {
private String id;
private String nama;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
}
File TestService.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author adildramdan
*/
public class TestService {
public void save(TestObject testObject) {
try {
Connection conn = OdbcConnection.getConnection();
Statement st = conn.createStatement();
st.executeUpdate("insert into survey (id,name ) values (" + testObject.getId() + ",'" + testObject.getNama() + "')");
st.close();
} catch (Exception ex) {
Logger.getLogger(TestService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void update(TestObject testObject) {
/**
* sama kaya yang insert tp quertynya update
*
*/
}
public void delete(Long id) {
/*
sama kaya insert tp query nya delete
*/
}
public TestObject getById(Long id) {
TestObject testObject = new TestObject();
try {
Connection conn = OdbcConnection.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM table where id= "+id+"");
while (rs.next()) {
testObject.setId(rs.getString("nama_field_id_di_database"));
testObject.setNama(rs.getString("nama_field_nama_di_database"));
}
} catch (Exception ex) {
Logger.getLogger(TestService.class.getName()).log(Level.SEVERE, null, ex);
}
return testObject;
}
public List<TestObject> findAll(){
List<TestObject> testObjects = new ArrayList<>();
try {
Connection conn = OdbcConnection.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM table ");
while (rs.next()) {
TestObject testObject = new TestObject();
testObject.setId(rs.getString("nama_field_id_di_database"));
testObject.setNama(rs.getString("nama_field_nama_di_database"));
testObjects.add(testObject);
}
} catch (Exception ex) {
Logger.getLogger(TestService.class.getName()).log(Level.SEVERE, null, ex);
}
return testObjects;
}
}
File MainApp.java
import java.util.List;
/**
*
* @author adildramdan
*/
public class MainApp {
public static void main(String[] args) {
/**
* test insert
*/
TestService testService = new TestService();
TestObject testObject = new TestObject();
testObject.setId("1");
testObject.setNama("test");
testService.save(testObject);
/**
* find all
*/
List<TestObject> findAll = testService.findAll();
/**
* find by id
*/
TestObject testObjectById = testService.getById(1L);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment