PostgreSQL - Multiple Result JDBC Postgres.

   
Vista:
Imágen de perfil de outrera

Multiple Result JDBC Postgres.

Publicado por outrera (35 intervenciones) el 29/04/2014 19:49:29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package conexion.pruebas;
 
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
 
 
public class CursoresTest2 {
 
	public static void main(String[] args) {
		System.out.println("-------- PostgreSQL "
				+ "JDBC Connection Testing ------------");
 
		try {
 
			Class.forName("org.postgresql.Driver");
 
		} catch (ClassNotFoundException e) {
 
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			return;
 
		}
 
		System.out.println("PostgreSQL JDBC Driver Registered!");
 
		Connection conn = null;
 
		try {
 
			conn = DriverManager.getConnection(
					"jdbc:postgresql://localhos/dbname", "username",
					"12345678");
 
			// We must be inside a transaction for cursors to work.
			conn.setAutoCommit(false);
 
			// Procedure call.
			CallableStatement cs = conn.prepareCall("{ call acceso.pss_listar_estado(?,?) }");
            cs.setInt(1, 245);
            cs.setInt(2, 48);
 
 
 
 
			// Execute the CALL statement and expecting multiple result sets
		      boolean isResultSet = cs.execute();
 
		// First ReulstSet object
		      if (!isResultSet) {
		        System.out.println("The first result is not a ResultSet.");
		        return;
		      }
 
		// First ReulstSet object
		      System.out.println("tabla estado:");
		      ResultSet res = cs.getResultSet();
		//primer cursor    
		     if (res.next() ){
		      	ResultSet rs = (ResultSet) res.getObject(1);
 
		      	while (rs.next()) {
		      		System.out.println(rs.getString("descripcion"));
 
		      	}
 
		      	rs.close();
 
		     };
 
			//segundo cursor    
		     if (res.next() ){
		      	ResultSet re = (ResultSet) res.getObject(1);
 
		      	while (re.next()) {
		      		System.out.println(re.getString(1));
 
		      	}
 
		      	re.close();
 
		     };
 
 
		     res.close();
 
//		// Move to the next result
//		      isResultSet = cs.getMoreResults();
//		      if (!isResultSet) {
//		        System.out.println("The next result is not more ResultSet.");
//		        return;
//		      }
//
//		// Second ReulstSet object
//		      System.out.println("errortable:");
//		      res = cs.getResultSet();
//		      while (res.next()) {
//		        System.out.println("  "+res.getString(1)
//		          +", "+res.getString(2));
//		        
//		      }
//		      res.close();
//		      
//		// Retrieve OUT parameters
//		     // System.out.println("Total number of records: "+cs.getInt(1));
 
 
 
 
		   // Close resource
		      cs.close();
 
 
 
 
		} catch (SQLException e) {
 
			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return;
 
		}
 
		if (conn != null) {
			System.out.println("You made it, take control your database now!");
		} else {
			System.out.println("Failed to make connection!");
		}
 
 
	}
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder