Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. import org.dao.AutorDao;
  2. import org.dao.MyDAO;
  3. import org.skife.jdbi.v2.DBI;
  4. //import org.skife.jdbi.v2.Handle;
  5. //import org.skife.jdbi.v2.sqlobject.Bind;
  6.  
  7. public class TestJdbi2 {
  8.  
  9. public static void main(String[] args) {
  10. DBI dbi = new DBI("jdbc:oracle:thin:@148.215.1.155:1521:DBSRALYC",
  11. "uautores",
  12. "uautores155");
  13.  
  14. MyDAO dao = dbi.open(MyDAO.class);
  15. System.out.println(dao.findNameById(194));
  16.  
  17.  
  18. try{
  19. AutorDao autor= dbi.open(AutorDao.class);
  20. System.out.println(autor.findById(194).getNombreAutor()+" "+autor.findById(194).getApellidoAutor());
  21. }
  22. catch(Exception ex){
  23. ex.printStackTrace();
  24. }
  25.  
  26. dao.close();
  27. }
  28.  
  29. }
  30.  
  31.  
  32.  
  33. package org.dao;
  34.  
  35. //import org.dao.ModeloAutorDAO;
  36. //import org.dao.ModeloAutorMapper;
  37. //import org.modelos.ModeloAutor;
  38. //import org.skife.jdbi.v2.BeanMapper;
  39. //import org.skife.jdbi.v2.DBI;
  40. //import org.skife.jdbi.v2.Handle;
  41. import org.skife.jdbi.v2.sqlobject.Bind;
  42. import org.skife.jdbi.v2.sqlobject.SqlQuery;
  43. import org.skife.jdbi.v2.sqlobject.SqlUpdate;
  44. //import org.skife.jdbi.v2.tweak.HandleCallback;
  45. //import org.skife.jdbi.v2.util.IntegerMapper;
  46. //import org.skife.jdbi.v2.sqlobject.Bind;
  47. //import org.skife.jdbi.v2.sqlobject.SqlQuery;
  48. //import org.skife.jdbi.v2.sqlobject.SqlUpdate;
  49. //import org.skife.jdbi.v2.sqlobject.customizers.Mapper;
  50. //import org.skife.jdbi.v2.sqlobject.mixins.Transactional;
  51. //import org.skife.jdbi.v2.util.StringMapper;
  52.  
  53. public interface MyDAO {
  54.  
  55. // @SqlUpdate("create table something (id int primary key, name varchar(100))")
  56. // void createSomethingTable();
  57.  
  58. // @SqlUpdate("insert into something (id, name) values (:id, :name)")
  59. // void insert(@Bind("id") int id, @Bind("name") String name);
  60.  
  61. @SqlQuery("SELECT NOMAUTNOR FROM TBLAUTNOR WHERE CVEAUTNOR= :id")
  62. String findNameById(@Bind("id") int id);
  63.  
  64. /**
  65. * close with no args is used to close the connection
  66. */
  67. void close();
  68.  
  69. }
  70.  
  71. package org.tutorial;
  72.  
  73. import java.io.Serializable;
  74.  
  75. import javax.persistence.*;
  76.  
  77.  
  78. @Entity
  79. public class Autor implements Serializable {
  80.  
  81. private static final long serialVersionUID = 1L;
  82.  
  83. @Id @GeneratedValue
  84. private long id;
  85.  
  86. private String nombreAutor;
  87. private String apellidoAutor;
  88. private String correoAutor;
  89. private String cveInstitucion;
  90. private String cveOrcid;
  91.  
  92. public Autor(){}
  93.  
  94.  
  95.  
  96.  
  97. public Autor(long id, String nombreAutor, String apellidoAutor,
  98. String correoAutor, String cveInstitucion, String cveOrcid) {
  99. super();
  100. this.id = id;
  101. this.nombreAutor = nombreAutor;
  102. this.apellidoAutor = apellidoAutor;
  103. this.correoAutor = correoAutor;
  104. this.cveInstitucion = cveInstitucion;
  105. this.cveOrcid = cveOrcid;
  106. }
  107.  
  108.  
  109.  
  110.  
  111. public String getCveOrcid() {
  112. return cveOrcid;
  113. }
  114.  
  115.  
  116.  
  117.  
  118. public void setCveOrcid(String cveOrcid) {
  119. this.cveOrcid = cveOrcid;
  120. }
  121.  
  122.  
  123.  
  124.  
  125. public String getCveInstitucion() {
  126. return cveInstitucion;
  127. }
  128.  
  129.  
  130.  
  131.  
  132. public void setCveInstitucion(String cveInstitucion) {
  133. this.cveInstitucion = cveInstitucion;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. public String getApellidoAutor() {
  140. return apellidoAutor;
  141. }
  142.  
  143.  
  144. public void setApellidoAutor(String apellidoAutor) {
  145. this.apellidoAutor = apellidoAutor;
  146. }
  147.  
  148. public long getId() {
  149. return id;
  150. }
  151. public void setId(long id) {
  152. this.id = id;
  153. }
  154. public String getNombreAutor() {
  155. return nombreAutor;
  156. }
  157. public void setNombreAutor(String nombreAutor) {
  158. this.nombreAutor = nombreAutor;
  159. }
  160. public String getCorreoAutor() {
  161. return correoAutor;
  162. }
  163. public void setCorreoAutor(String correoAutor) {
  164. this.correoAutor = correoAutor;
  165. }
  166.  
  167. public static long getSerialversionuid() {
  168. return serialVersionUID;
  169. }
  170.  
  171. @Override
  172. public String toString() {
  173. return String.format("Autor(%d%n, %s , %s, %s , %s,%s)", this.id, this.nombreAutor,this.apellidoAutor,this.correoAutor,this.cveInstitucion,this.cveOrcid);
  174. }
  175. }
  176.  
  177.  
  178. package org.modelos;
  179.  
  180. public class ModeloAutor implements java.io.Serializable {
  181. /**
  182. *
  183. */
  184. private static final long serialVersionUID = 1L;
  185. private long id;
  186. private String nombreAutor;
  187. private String apellidoAutor;
  188. private String correoAutor;
  189. private String cveInstitucion;
  190. private String cveOrcid;
  191.  
  192. public ModeloAutor(){}
  193.  
  194.  
  195.  
  196. public ModeloAutor(long id, String nombreAutor, String apellidoAutor,
  197. String correoAutor, String cveInstitucion, String cveOrcid) {
  198. super();
  199. this.id = id;
  200. this.nombreAutor = nombreAutor;
  201. this.apellidoAutor = apellidoAutor;
  202. this.correoAutor = correoAutor;
  203. this.cveInstitucion = cveInstitucion;
  204. this.cveOrcid = cveOrcid;
  205. }
  206.  
  207.  
  208.  
  209. public long getId() {
  210. return id;
  211. }
  212.  
  213. public void setId(long id) {
  214. this.id = id;
  215. }
  216.  
  217. public String getNombreAutor() {
  218. return nombreAutor;
  219. }
  220.  
  221. public void setNombreAutor(String nombreAutor) {
  222. this.nombreAutor = nombreAutor;
  223. }
  224.  
  225. public String getApellidoAutor() {
  226. return apellidoAutor;
  227. }
  228.  
  229. public void setApellidoAutor(String apellidoAutor) {
  230. this.apellidoAutor = apellidoAutor;
  231. }
  232.  
  233. public String getCorreoAutor() {
  234. return correoAutor;
  235. }
  236.  
  237. public void setCorreoAutor(String correoAutor) {
  238. this.correoAutor = correoAutor;
  239. }
  240.  
  241. public String getCveInstitucion() {
  242. return cveInstitucion;
  243. }
  244.  
  245. public void setCveInstitucion(String cveInstitucion) {
  246. this.cveInstitucion = cveInstitucion;
  247. }
  248.  
  249. public String getCveOrcid() {
  250. return cveOrcid;
  251. }
  252.  
  253. public void setCveOrcid(String cveOrcid) {
  254. this.cveOrcid = cveOrcid;
  255. }
  256.  
  257.  
  258. }
  259.  
  260.  
  261. package org.dao;
  262.  
  263. import org.modelos.ModeloAutor;
  264. //import org.skife.jdbi.v2.StatementContext;
  265. import org.skife.jdbi.v2.sqlobject.Bind;
  266. import org.skife.jdbi.v2.sqlobject.SqlQuery;
  267. import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
  268. //import org.skife.jdbi.v2.tweak.ResultSetMapper;
  269. //import org.skife.jdbi.v2.sqlobject.customizers.*;
  270.  
  271. @RegisterMapper(ModeloAutorMapper.class)
  272. public interface AutorDao {
  273.  
  274.  
  275. @SqlQuery("SELECT CVEAUTNOR, NOMAUTNOR,APEAUTNOR,MAILAUTNOR1,CVEENTINT,CVEORCID FROM TBLAUTNOR WHERE CVEAUTNOR = :id")
  276. ModeloAutor findById(@Bind("id") long id);
  277.  
  278. }
  279.  
  280.  
  281. package org.modelos;
  282.  
  283. import java.sql.ResultSet;
  284. import java.sql.SQLException;
  285.  
  286. import org.skife.jdbi.v2.StatementContext;
  287. import org.skife.jdbi.v2.tweak.ResultSetMapper;
  288.  
  289. public abstract class ModeloAutorMapper implements ResultSetMapper<ModeloAutor> {
  290.  
  291. public ModeloAutor map(int index, ResultSet r, StatementContext ctx) throws SQLException
  292. {
  293. return new ModeloAutor(
  294. r.getLong("CVEAUTNOR"),
  295. r.getString("NOMAUTNOR")
  296. ,r.getString("APEAUTNOR")
  297. ,r.getString("MAILAUTNOR1")
  298. ,r.getString("CVEENTINT")
  299. ,r.getString("CVEORCID")
  300. );
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement