I have 3 tables having the following content :
Author
idAuthor INT
name VARCHAR
Publication
idPublication INT
Title VARCHAR
Date YEAR
Type VARCHAR
Conference
author_has_publication
author_idAuthor INT
publication_idPublication INT
I am trying to do relational schema on the authors. The objectif is to show the number of publication they have in common. The authors name are parameters, I can have up to 8 names. My code is giving the number of common publication between 2 authors, so i have to loop it. I am currently using a Java loop and SQL statement to do that. Here is the SQL part
private int runQuery(String a1, String a2){ // a1 author 1 and a2 author 2
try {
auth1 = new ArrayList<String>();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "ROOT");
Statement stmt = connection.createStatement();
long start = System.currentTimeMillis();
String queryUpdate1 = "DROP TABLE IF EXISTS temp1;";
String queryUpdate2 = "DROP TABLE IF EXISTS temp2;";
String queryUpdate3 = "CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS (SELECT Author.name, Publication.idPublication, Publication.title FROM Author INNER JOIN Author_has_Publication ON Author_has_Publication.author_idAuthor=author.idAuthor INNER JOIN Publication ON Author_has_Publication.publication_idPublication=publication.idPublication WHERE Author.name='"+ a1+"');";
String queryUpdate4 = "CREATE TEMPORARY TABLE IF NOT EXISTS temp2 AS (SELECT Author.name, Publication.idPublication, Publication.title FROM Author INNER JOIN Author_has_Publication ON Author_has_Publication.author_idAuthor=author.idAuthor INNER JOIN Publication ON Author_has_Publication.publication_idPublication=publication.idPublication WHERE Author.name='"+ a2+"');";
String query = "SELECT COUNT(*) FROM (SELECT temp1.title from temp1 INNER JOIN temp2 on temp1.idPublication = temp2.idPublication) as t;";
stmt.executeUpdate(queryUpdate1);
stmt.executeUpdate(queryUpdate2);
stmt.executeUpdate(queryUpdate3);
stmt.executeUpdate(queryUpdate4);
ResultSet rs = stmt.executeQuery(query);
int result = -1;
while (rs.next()) {
result = rs.getInt(1);
}
System.out.println("result = " + result);
long end = System.currentTimeMillis() - start;
queryTimeLabel.setText("Query Execution Time :"+end);
connection.close();
return result;
} catch (Exception e) {
System.out.println(e);
}
return -1;
}
Here is the loop part (to repeat the SQL when there are more than 2 authors given) and generate the graph :
public void actionPerformed(ActionEvent e) {
graph = new mxGraph();
Object parent = graph.getDefaultParent();
authVertex = getAuthors();
// ///////////////////////////////////
// CREATES GRAPH, Graph only shows up after you resize the window
graph.getModel().beginUpdate();
try {
int i = 0;
for(String a: authVertex.keySet()){
int j = 0;
for(String b: authVertex.keySet()){
if(j > i) {
graph.insertEdge(parent, null, String.valueOf(runQuery(a,b)), authVertex.get(a), authVertex.get(b)); // loop the SQL statement 2 by 2.
}
j++;
}
i++;
}
} finally {
graph.getModel().endUpdate();
}
graphComponent = new mxGraphComponent(graph);
graphPan.removeAll();
graphPan.add(graphComponent);
setVisible(true);
// /////////////////////////////////////////
}
My code is currently working, but I would like to know if it was possible to increase the performance by passing everything into MySQL, that means that I enter the authors name in parameter and the loop is hangled by MySQL, I check the MySQL procedure but my issue is how to handle the authors names parameter as it is a variable.
One way, in a single statement:
SELECT COUNT(*)
FROM Author_has_Publication AS ap1
JOIN Author_has_Publication AS ap2 ON ap1.publication_idPublication =
ap2.publication_idPublication
JOIN Author AS a1 ON ap1.author_idAuthor = a1.id_Author
JOIN Author AS a2 ON ap2.author_idAuthor = a2.id_Author
WHERE a1.name = '...'
AND a2.name = '...'
Another way may be
SELECT COUNT(*)
FROM
(
SELECT ahp.publication_idPublication, COUNT(*)
FROM Author_has_Publication AS ahp
JOIN Author AS a ON a.id_Author = ahp.author_idAuthor
WHERE a.name IN ('...', '...')
GROUP BY ahp.publication_idPublication
HAVING COUNT(*) = 2 -- Number of authors
) x
Composite indexes needed:
Author_has_Publication: (author_idAuthor, publication_idPublication)
Author_has_Publication: (publication_idPublication, author_idAuthor)
Author: (name, id)
Note: Each technique can be rather easily extended to more than 2 authors. The second query could even be adapted to "at least 3 of these 5 authors": 5 names in IN and HAVING COUNT(*) >= 3.
Related
I created a query on MySQL workbench, it worked fine, but when I tried to show that query on Java it does not work. I'm new to this, and using classicmodels Db. The following query that worked on workbench:
select c.customerNumber, c.customerName,
o.orderNumber,
d.productCode, d.quantityOrdered, d.priceEach, (d.quantityOrdered * d.priceEach) as totalPrice,
e.employeeNumber, e.lastName, ((d.quantityOrdered * d.priceEach)*0.01) as commission
from customers as c inner join orders as o on c.customerNumber=o.customerNumber inner join
orderdetails as d on o.orderNumber=d.orderNumber
inner join employees as e on c.salesRepEmployeeNumber=e.employeeNumber group by productCode;
'486', 'Motor Mint Distributors Inc.', '10109', 'S18_1129', '26', '117.48', '3054.48', '1323', 'Vanauf', '30.5448'
Here is my Java code:
public void query() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(urlConn, user, passWord);
pdState = conn.prepareStatement("SELECT c.customerNumber, c.customerName,o.orderNumber,"
+ " d.productCode, d.quantityOrdered, d.priceEach,(d.quantityOrdered * d.priceEach) as totalPrice,"
+ " e.employeeNumber, e.lastName,((d.quantityOrdered * d.priceEach)*0.01) as commission"
+ " FROM customers as c inner join orders as o on c.customerNumber=o.customerNumber"
+ " inner join orderdetails as d on o.orderNumber=d.orderNumber"
+ " inner join employees as e on c.salesRepEmployeeNumber=e.employeeNumbergroup by productCode");
rSet = pdState.executeQuery();
ResultSetMetaData rsmd = rSet.getMetaData();
c = rsmd.getColumnCount();
DefaultTableModel dtModel = (DefaultTableModel) tblQuery.getModel();
dtModel.setRowCount(0);
while (rSet.next()) {
#SuppressWarnings("UseOfObsoleteCollectionType")
Vector vector = new Vector();
for (int i = 1; i <= c; i++) {
vector.add(rSet.getString("customerNumber"));
vector.add(rSet.getString("customerName"));
vector.add(rSet.getString("orderNumber"));
vector.add(rSet.getString("productCode"));
vector.add(rSet.getString("quantityOrdered"));
vector.add(rSet.getString("priceEach"));
vector.add(rSet.getString("totalPrice"));
vector.add(rSet.getString("employeeNumber"));
vector.add(rSet.getString("lastName"));
vector.add(rSet.getString("commission"));
}//end of for loop
dtModel.addRow(vector);
}//end of while loop
} catch (ClassNotFoundException | SQLException e) {
e.getMessage();
}//end of try and catch block
}//end of query
The problem here is the group by clause. You've grouped on one field, and failed to ensure all the other fields are either uniquely determined by that field, or grouped functions. Remove the group by productCode and everything should work.
I think the reason it "worked" in MySQL workbench is probably that there is an option you can set (but probably should not) that allows improper grouped fields to "work" (I think by just picking one of many values).
As an example of a query that might work, the following should report the commission each employee has earned by product sold. Since the customer is irrelevant to that information, that table wasn't queried.
select d.productCode, e.employeeNumber, e.lastName,
sum((d.quantityOrdered * d.priceEach)*0.01) as commission
from orders as o
inner join orderdetails as d on o.orderNumber=d.orderNumber
inner join employees as e on c.salesRepEmployeeNumber=e.employeeNumber
group by productCode, employeeNumber;
I'm new to java and I need help with displaying a joined table/query in jtable.
First, I have done displaying data from 1 table which is:
Select data from 1 table
insert the result to its entity and insert each one of it to a List
return the list to view and insert row to jtable
I am using a DAO pattern, which has a factory, interface, implement, entity and view.
So what if I select data from other table?
Here is my get method in implement for getting book
public List get(String find) {
try {
ps = db.connect().prepareStatement("SELECT * FROM books WHERE title like ? ");
ps.setString(1, "%" + find + "%");
status = db.execute(ps);
if (status) {
books = db.get_result();
listBooks = new ArrayList<>();
while (books.next()) {
entity_books b = new entity_books();
b.setId(books.getInt(1));
b.setId_category(books.getInt(2));
b.setTitle(books.getString(3));
listBooks.add(b);
}
books.close();
return listBooks;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
and then in my view:
listBooks = booksDAO.get(find.getText());
model = (DefaultTableModel) book_table.getModel();
model.setRowCount(0);
listBooks.forEach((data) -> {
model.addRow(new Object[]{
data.getId(),
data.getId_category(),
data.getTitle(),
});
});
This works fine, but I want the query to join table so I can see the category name instead of just ID category. I can do the query, but how do I apply that to my code?
Here is the query for joined table
select title,category from book b
join category c on c.id = b.id_category
Normally if I select only 1 table, I would insert it to its entity ( book table -> book entity ), so how do I handle this with multiple tables?
I didn't use prepared statement, but this code works on my end.
String sql = "SELECT * FROM customer c JOIN company cmp ON c.company_idcompany = cmp.idcompany";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while (rs.next()) {
//Retrieve this from customer table
int id = rs.getInt("idcustomer");
//Retrieve this from customer table
String username = rs.getString("company_username");
//Display values
System.out.println("ID: " + id);
System.out.println("Username: " + username);
}
I have created a stored proc in a sql server db that appears to be executing properly for one record. The code is shown below:
try {
String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String username="username";
String url="jdbc:sqlserver://someDB:8808;SelectMethod=cursor";
String password="password";
Class.forName(className);
conn = DriverManager.getConnection(url, username, password);
// need to obtain store procedure in db(a sql script stored in our sql server db)
CallableStatement cstmt = conn.prepareCall("{call dbo.sp_SomeProc(?,?,?,?,?,?,?,?,?)}");
cstmt.setString (1,"id34234, id34246, id234234");
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(3, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(7, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(8, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(9, java.sql.Types.VARCHAR);
int rowsAffected = 0;
boolean isResults = cstmt.execute();
while(isResults || rowsAffected != -1) {
if(isResults) {
rs = cstmt.getResultSet();
break;
}else {
rowsAffected = cstmt.getUpdateCount();
}
isResults = cstmt.getMoreResults();
}
while(rs.next()) {
slInfo.add(cstmt.getString(2));
slInfo.add(cstmt.getString(3));
slInfo.add(cstmt.getString(4));
slInfo.add(cstmt.getString(5));
slInfo.add(cstmt.getString(6));
slInfo.add(cstmt.getString(7));
slInfo.add(cstmt.getString(8));
slInfo.add(cstmt.getString(9));
}
writeToFile(outFileName, slInfo);
}catch(SQLException ex) {
ex.printStackTrace();
}catch(ClassNotFoundException ex) {
ex.printStackTrace();
}finally {
try { cstmt.close(); } catch (Exception e) { /* ignored */ }
try { conn.close(); } catch (Exception e) { /* ignored */ }
}
The issue is since I have more than one record, I need to obtain a result set. However, every attempt other than executeUpdate() results in an exception.
My first attempt:
ResultSet rs = cstmt.executeQuery() // instead cstmt.executeUpdate()
the exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
Second attempt:
cstmt.execute();
ResultSet rs = (ResultSet)cstmt.getObject(1) // not actually sure what index to use here
It appears that the first attempt is what I have seen other people do, but I can't figure out what I am doing wrong. Any help would be most appreciated.
UPDATE
Here is the stored proc:
USE [someDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetAvatarInfo]
(
#idList VARCHAR(MAX),
#uniqueIdent VARCHAR(64) OUTPUT,
#FULLNAME VARCHAR(128) OUTPUT,
#CCNUMSEQ VARCHAR(64) OUTPUT,
#ALIASTYPE VARCHAR(64) OUTPUT,
#SLNUMBER VARCHAR(64) OUTPUT,
#TISSUETYPE VARCHAR(64) OUTPUT,
#SAMPLESUBTYPE VARCHAR(64) OUTPUT,
#SUBMITTEDDIAG VARCHAR(64) OUTPUT
)
AS
BEGIN
DECLARE #idAvatarEvent INT,
#avatarDisease INT;
SELECT #idAvatarEvent = idEventType
FROM CCR.dbo.EventType
WHERE codeCancerGroup = 'TCC' AND eventType = 'Avatar';
--Create table to hold all SL ids that are involved in the search
IF (OBJECT_ID('tempdb.dbo.#SLList') IS NOT NULL) DROP TABLE #SLList;
CREATE TABLE #SLList
(
idAlias INT NOT NULL IDENTITY (1,1)
,SLNumber VARCHar(100)
)
INSERT INTO #SLList (SLNumber)
SELECT [str]
FROM BST.dbo.fn_parseString(#idList, ',');
SELECT #avatarDisease = ATT.idAttribute
FROM CCR.dbo.Attribute ATT
JOIN CCR.dbo.AttributeContainer ATC ON ATT.idAttributeContainer = ATC.idAttributeContainer
WHERE ATT.attributeName = 'avatarDiseaseGroup'
AND ATC.containerName = 'Avatar'
SELECT #PERMRN = per.mrn,
#FULLNAME = per.firstName + ' ' + per.lastName,
#CCNUMSEQ = ali.ccNumberSeq,
#ALIASTYPE = sat.aliasType,
#SLNUMBER = sl.SLNumber,
#TISSUETYPE = sst.description,
#SAMPLESUBTYPE = case when pt.preparationType LIKE '%Formalin%' THEN 'FFPE' ELSE pt.preparationType END,
#SUBMITTEDDIAG = ac.choice
FROM #SLList sl
LEFT JOIN DB1.dbo.AliquotAlias sa on sl.SLNumber = sa.alias
LEFT JOIN DB1.dbo.AliquotAliasType sat on sa.idAliasType = sat.idAliasType
LEFT JOIN DB1.dbo.VIEW_Aliquot2 ali on sa.idAliquot = ali.idAliquot
LEFT JOIN DB1.dbo.Sample sam on ali.idSample = sam.id
LEFT JOIN DB1.dbo.VIEW_SampleSubType sst on sam.codeSampleSubType = sst.codeSampleSubType
LEFT JOIN DB1.dbo.PreparationType pt on sam.idPreparationType = pt.id
LEFT JOIN DB1.dbo.BSTCollection col on sam.idBSTCollection = col.idBSTCollection
LEFT JOIN DB1.dbo.BSTPatient pat on col.idBSTPatient = pat.idBSTPatient
LEFT JOIN DB2.dbo.Person per on pat.idPerson = per.idPerson
LEFT JOIN DB3.dbo.Patient cpat on per.idPerson = cpat.idPerson
LEFT JOIN DB3.dbo.PatientCancerGroup pcg on cpat.idPatient = pcg.idPatient AND pcg.codeCancerGroup = 'TCC'
LEFT JOIN DB3.dbo.MedicalEvent med on pcg.idPatient = med.idPatient AND pcg.codeCancerGroup = med.codeCancerGroup AND med.idEventType = #idAvatarEvent
LEFT JOIN DB3.dbo.AttributeValue av on med.idAttributeValueSet = av.idAttributeValueSet AND av.idAttribute = #avatarDisease
LEFT JOIN DB3.dbo.AttributeChoice ac on av.valueIdAttributeChoice = ac.idAttributeChoice
ORDER BY MRN
END;
You need to modify the SP to return a result set, because the executeQuery() method expects the SP to return a result set.
Assuming that all other parts of the SP are correct (I could not test it on my PC for various reasons), here's the SP that should work:
USE [someDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetAvatarInfo]
(
#idList VARCHAR(MAX)
)
AS
BEGIN
DECLARE #idAvatarEvent INT,
#avatarDisease INT;
SELECT #idAvatarEvent = idEventType
FROM CCR.dbo.EventType
WHERE codeCancerGroup = 'TCC' AND eventType = 'Avatar';
--Create table to hold all SL ids that are involved in the search
IF (OBJECT_ID('tempdb.dbo.#SLList') IS NOT NULL) DROP TABLE #SLList;
CREATE TABLE #SLList
(
idAlias INT NOT NULL IDENTITY (1,1)
,SLNumber VARCHar(100)
)
INSERT INTO #SLList (SLNumber)
SELECT [str]
FROM BST.dbo.fn_parseString(#idList, ',');
SELECT #avatarDisease = ATT.idAttribute
FROM CCR.dbo.Attribute ATT
JOIN CCR.dbo.AttributeContainer ATC ON ATT.idAttributeContainer = ATC.idAttributeContainer
WHERE ATT.attributeName = 'avatarDiseaseGroup'
AND ATC.containerName = 'Avatar'
SELECT per.mrn 'PERMRN',
per.firstName + ' ' + per.lastName 'FULLNAME',
ali.ccNumberSeq 'CCNUMSEQ',
sat.aliasType 'ALIASTYPE`',
sl.SLNumber 'SLNUMBER',
sst.description 'TISSUETYPE',
case when pt.preparationType LIKE '%Formalin%' THEN 'FFPE' ELSE pt.preparationType END 'SAMPLESUBTYPE',
ac.choice 'SUBMITTEDDIAG'
FROM #SLList sl
LEFT JOIN DB1.dbo.AliquotAlias sa on sl.SLNumber = sa.alias
LEFT JOIN DB1.dbo.AliquotAliasType sat on sa.idAliasType = sat.idAliasType
LEFT JOIN DB1.dbo.VIEW_Aliquot2 ali on sa.idAliquot = ali.idAliquot
LEFT JOIN DB1.dbo.Sample sam on ali.idSample = sam.id
LEFT JOIN DB1.dbo.VIEW_SampleSubType sst on sam.codeSampleSubType = sst.codeSampleSubType
LEFT JOIN DB1.dbo.PreparationType pt on sam.idPreparationType = pt.id
LEFT JOIN DB1.dbo.BSTCollection col on sam.idBSTCollection = col.idBSTCollection
LEFT JOIN DB1.dbo.BSTPatient pat on col.idBSTPatient = pat.idBSTPatient
LEFT JOIN DB2.dbo.Person per on pat.idPerson = per.idPerson
LEFT JOIN DB3.dbo.Patient cpat on per.idPerson = cpat.idPerson
LEFT JOIN DB3.dbo.PatientCancerGroup pcg on cpat.idPatient = pcg.idPatient AND pcg.codeCancerGroup = 'TCC'
LEFT JOIN DB3.dbo.MedicalEvent med on pcg.idPatient = med.idPatient AND pcg.codeCancerGroup = med.codeCancerGroup AND med.idEventType = #idAvatarEvent
LEFT JOIN DB3.dbo.AttributeValue av on med.idAttributeValueSet = av.idAttributeValueSet AND av.idAttribute = #avatarDisease
LEFT JOIN DB3.dbo.AttributeChoice ac on av.valueIdAttributeChoice = ac.idAttributeChoice
ORDER BY per.mrn
END;
I have 2 database tables for my program: GameInfo and Characters.
How this works is that a Game has 4 maps with different names and each character added to the game must be assigned any of the 4 maps. Now I have the sql statement which returns a result set named "Expr1001, of the number of characters on each map. I then need to add this information to a jTable and link up the corresponding amount of each characterson a map, with the mapname.
My ResultSet with the query which returns the amount of characters on each map:
ResultSet qs = dbm.queryDatabase("SELECT Expr1001 FROM (SELECT GameInfo.mapname, SUM(IIF(Map = GameInfo.mapname,1,0)) FROM (SELECT * FROM [Character] INNER JOIN Player ON Character.PlayerID=Player.[ID]) AS A RIGHT JOIN GameInfo ON A.Character.map = GameInfo.mapname GROUP BY GameInfo.mapname) AS [%$###_Alias]");
The whole method which gets the Game Info from the database from the GameInfo table, which comprises of a GameID and MapName only.
public Game[] getGameInfo(){
Game[] arr = null; //Creates an array of Games
try { //getting list from database
ResultSet rs = dbm.queryDatabase("Select Count(GameID) as NumGames from GameInfo" );
//While there are still more rows to read from the database.
rs.next();
int count = rs.getInt("NumGames");
arr = new Game[count];
String sql = "Select * from GameInfo";
// System.out.println(sql);
rs = dbm.queryDatabase(sql);
//Take the info from the current row
//Add the info to the array
ResultSet qs = dbm.queryDatabase("SELECT Expr1001 FROM (SELECT GameInfo.mapname, SUM(IIF(Map = GameInfo.mapname,1,0)) FROM (SELECT * FROM [Character] INNER JOIN Player ON Character.PlayerID=Player.[ID]) AS A RIGHT JOIN GameInfo ON A.Character.map = GameInfo.mapname GROUP BY GameInfo.mapname) AS [%$###_Alias]");
for(int i = 0; rs.next(); i++){
arr[i] = new Game(
rs.getInt("GameInfo.GameID"),
rs.getString("GameInfo.mapname"),
qs.getInt(i));
}//Creates a Game from the currently selected info
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Failed to get Games");
e.printStackTrace();
}
return arr;
}
}
The data is then added to the jTable which is on a Panel in the GameInfoPanel class:
public void refreshTable() {
//remove old stuff
refreshing = true;
Game[] arr = gim.getGameInfo();
DefaultTableModel model = (DefaultTableModel) GameInfoTable.getModel();
while (model.getRowCount() > 0) {
model.removeRow(0);
}
for (int i = 0; i < arr.length; i++) {
model.addRow(new Object[]{
arr[i].getNumberOfCharacters(),
arr[i].getID(),
arr[i].getMapName()});
}
refreshing = false;
//load new data from database using manager
}
I keep getting the error which points to the ResultSet qs line: "user lacks privilege or object not found: A.CHARACTER.MAP" when I try and run the program even though when I copy this statement into Microsoft Access and run it, it's fine.
Help please!
Thanks.
(I am still at school so not really a genius on this at all so please have mercy if I've done some stupid things)
Don't run a select count(*) first to get the number of games for allocating an array. Build you result in a List, which will auto-expand as needed. You can always convert the list to an array later, if needed.
Don't run two queries when one can do the job, especially when you already join to the table in question.
Your SQL is unreadable, so here it is in a more readable format:
String sql = "SELECT Expr1001" +
" FROM (SELECT GameInfo.mapname" +
", SUM(IIF(Map = GameInfo.mapname,1,0))" +
" FROM (SELECT *" +
" FROM [Character]" +
" INNER JOIN Player ON Character.PlayerID=Player.[ID]" +
") AS A" +
" RIGHT JOIN GameInfo ON A.Character.map = GameInfo.mapname" +
" GROUP BY GameInfo.mapname" +
") AS [%$###_Alias]";
The outer query does nothing. Get rid of it.
Don't SELECT *. Select the columns you want, i.e. Character.map.
Since you want GameID, add it to the GROUP BY.
Specify an alias for the SUM value.
public Game[] getGameInfo(){
String sql = " SELECT GameInfo.GameID" +
", GameInfo.mapname" +
", SUM(IIF(C.map = GameInfo.mapname,1,0)) AS CharacterCount" +
" FROM ( SELECT Character.map" +
" FROM [Character]" +
" JOIN Player ON Player.[ID] = Character.PlayerID" +
") C" +
" RIGHT JOIN GameInfo ON GameInfo.mapname = C.map" +
" GROUP BY GameInfo.GameID" +
", GameInfo.mapname";
try (ResultSet rs = dbm.queryDatabase(sql)) {
List<Game> games = new ArrayList<>();
while (rs.next())
games.add(new Game(rs.getInt("GameID"),
rs.getString("mapname"),
rs.getInt("CharacterCount")));
return games.toArray(new Game[games.size()]);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Failed to get Games");
e.printStackTrace();
return null;
}
}
public void ret_data(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName = mass_specc", "sa", "^mbwin1" );
//Create Statement object
m_Statement = con.createStatement();
String query = "SELECT a.cde_n18, count(a.cde_n18), c.repname_c250, d.name_c100, e.name_c250, f.regname FROM ballot as a INNER JOIN dataentry as b ON a.cde_n18 = b.cde_n18 INNER JOIN position_type as d ON a.position_n18 = d.code_n18 INNER JOIN gadi as c ON b.data_n18 = c.data_n18 INNER JOIN coop_gi as e ON c.cid_n18 = e.cid_n18 INNER JOIN regions as f ON e.region_id = f.regid GROUP BY a.cde_n18, b.data_n18, c.repname_c250, d.name_c100, e.name_c250, f.regname ORDER BY count (a.cde_n18) DESC";
//Execute the query
m_ResultSet = m_Statement.executeQuery(query);
//Loop through the results
while (m_ResultSet.next()) {
Vector <String> d=new Vector<String>();
d.add(m_ResultSet.getString("repname_c250"));
d.add(m_ResultSet.getString("regname"));
d.add(m_ResultSet.getString("name_c100"));
d.add(m_ResultSet.getString("cde_n18"));
data.add(d);
}
supply an ALIAS for simplicity on the aggregated field.
String query = "SELECT a.cde_n18, COUNT(a.cde_n18) totalCount, c.repname_c250,.."
and fetch the given alias,
d.add(m_ResultSet.getString("totalCount"));