Querying from a database
Hi,
I would like someone to help me coding the search from a database.
I have a html document which asks for a title or an author. My servlet should execute the query using the given title or author retrieving data from the database.
I am trying to execute this query which i know as in sql.
Select Authors.FirstName, Authors.LastName, Titles.Title from Titles, Authors, AuthorISBN where (FirstName = req.getParameter("keyword") OR Title =req.getParameter("keyword") ) AND Authors.AuthorID=AuthorISBN.AuthorID AND AuthorISBN.ISBN=Titles.ISBN
I am getting sqlexceptions if i execute this query in the jdbc. Please someone help me in coding this in jdbc appropriately.
Thank you.
You are getting errors because that string is not valid SQL. Write a string expression that does produce valid SQL, something like this:
String sql = "Select Authors.FirstName, Authors.LastName, Titles.Title from Titles, Authors, AuthorISBN where (FirstName = " + req.getParameter("keyword") + " OR Title = " + req.getParameter("keyword") + " ) AND Authors.AuthorID=AuthorISBN.AuthorID AND AuthorISBN.ISBN=Titles.ISBN;
You'll have to hack around with it to make it work. I am willing to bet you don't get it right the first time. Use System.out.println(sql) to check what you are producing.
Similar Messages
-
How To Save Modified Fields After query From The Database
I Have This Customer Information Form That's Sort of an Ordering Forms , I Made It By LiveCycle v9.0
And I Want To be able to search for Specific Record and Modify it and Save It Again to The DataBase
I believe That We All Saw The (( Data Connected Forms Presentation )) For Pual Guerette
At One Part Of The Presentation (Minute 0:26:00 specifically) When He Try To Search A Specific Record In the Database
And He Got all this annoying Messages Then The Record what He looking Shows up ; He Said That We No Longer able to Modify The Record After We Doing the Search
My First Question Is Why ???
Is there anyway After I got that Specific Record I Want from the Database I Can make my modification on it and update it Back in the DB.
OR i Have to Browse the whole DB looking for it Specially that I got this (ID Auto Increasment Number field Issue) Which leads me to ;
My Second Qustion;
Why I Can't just connect ''The Form Order Number'' to an Auto Increasment Number field in the DB
Obviously i can't, So Can I do Anything to achieve this goal ??
I Believe that we can Find some scripting to make that Happen
I Will Be So Grateful If There Is Any Helpful Answer
FYI: I'm Not Java Expert So If There Is any Coding That's Can be Helpful Please POST ALL OF IT if you can ,,,,Hi,
The information there is excellent and I would strongly recommend it.
I don't have any public database examples and I am not inclined to spend time preparing an example if you are not prepared to read up on the methods.
Stefan's site is the only link I have for databases.
Good luck,
Niall -
Hi
i have a table named "gatepass" in database A.
i created public synonym
(create public synonym gatepass for schema.gatepass@databaselink) in database B for the table "gatepass" in database A.
when i query that table in db B through SQL prompt, it fetches data.
but when i query that table through form, form hangs(goes into working state) and ends.
where is the problem
plz help
thanxi use forms 6i.
in your database B create a view:
create or replace view gatepass_v as
select * from schema.gatepass@databaselink
and then create a synonym public over the new view.
and in your form use the sinonym public or the view.
test please. -
SELECT query from multiple databases
Hi,
Is it possible to run a SELECT query using multiple DBs' tables, like outer joining them. One of the DBs is Sybase.
Thanks in advanceA TopLink session can span multiple databases if they can be accessed through a single connection. A single select would require the database to handle the aggregation as mentioned in the other post.
Alternatively TopLink's session broker will support making a single session present persistent types from multiple independent databases. This approach will not support a single SELECT spanning the databases though.
Doug -
Convert query from other database
Hi all,
Please help me,
I need convert this query for oracle, this originaly from postgres.
I appreciate anyone who can help me... thanks a lot
select
CASE catg_id
WHEN 1 THEN 'RESIDENCIAL'
WHEN 2 THEN 'COMERCIAL'
WHEN 3 THEN 'INDUSTRIAL'
WHEN 4 THEN 'PÚBLICO'
END as "Categoria",
sum(coalesce(vlr_arrec_tot,0)) as "Arrecadação Provisória Novembro_2010"
from
-- ARRECADAÇÃO DIÁRIA
select
coalesce(arrec1.catg_id,devol.catg_id) as catg_id,
sum(coalesce(arrec1,0) - coalesce(devol,0)) as vlr_arrec_tot
from
select catg_id,
sum(ardd.ardd_vlpagamentos) as arrec1
from arrecadacao.arrecadacao_dados_diarios ardd
where ardd.ardd_amreferenciaarrecadacao = 201011
group by 1
order by 1
) as arrec1
full outer join
select catg_id,
sum(dvdd.dvdd_vldevolucoes) as devol
from arrecadacao.devolucao_dados_diarios dvdd
where dvdd.dvdd_amreferenciaarrecadacao = 201011
and dvdd.dvdd_tipodevolucao in ('C','D','N')
group by 1
order by 1
) as devol on devol.catg_id = arrec1.catg_id
group by 1
order by 1
) as arrec
group by 1
order by 1saddones wrote:
Hi all,
Please help me,
I need convert this query for oracle, this originaly from postgres.
I appreciate anyone who can help me... thanks a lot
select
CASE catg_id
WHEN 1 THEN 'RESIDENCIAL'
WHEN 2 THEN 'COMERCIAL'
WHEN 3 THEN 'INDUSTRIAL'
WHEN 4 THEN 'PÚBLICO'
END as "Categoria",
sum(coalesce(vlr_arrec_tot,0)) as "Arrecadação Provisória Novembro_2010"
from
-- ARRECADAÇÃO DIÁRIA
select
coalesce(arrec1.catg_id,devol.catg_id) as catg_id,
sum(coalesce(arrec1,0) - coalesce(devol,0)) as vlr_arrec_tot
from
select catg_id,
sum(ardd.ardd_vlpagamentos) as arrec1
from arrecadacao.arrecadacao_dados_diarios ardd
where ardd.ardd_amreferenciaarrecadacao = 201011
group by 1
order by 1
) as arrec1
full outer join
select catg_id,
sum(dvdd.dvdd_vldevolucoes) as devol
from arrecadacao.devolucao_dados_diarios dvdd
where dvdd.dvdd_amreferenciaarrecadacao = 201011
and dvdd.dvdd_tipodevolucao in ('C','D','N')
group by 1
order by 1
) as devol on devol.catg_id = arrec1.catg_id
group by 1
order by 1
) as arrec
group by 1
order by 1What exactly about the query doesn't work now? It all looks compliant to me (though i'm not sure about the group by 1 stuff, you may or may not, have to put the actual column name). I also think your alias is too long (just based on visual inspection), it's going to need to be 30 chars or less
"Arrecadação Provisória Novembro_2010"Keep in mind that we don't have your tables (nor your Oracle version since we don't know it) so if you really want help you'll have to put in a little more effort to make this manageable. -
PC session hangs when launch a query from Oracle 9i client to 10g database
I have a pc with Oracle 9.2.0.1.0 client installed. The database server level is 10.1.0.5.
When I launch a query from a client session the pc sesssion locks up and doesn't return any results. When I launch the same query from the database server , or from other pcs with Oracle client 10 installed , I get results almost immediately from the query.
The pc in question is running Windows XP Service pack 2. Other queries run fine from the pc.
Are there are any known issues with Oracle 9 clients querying an Oracle 10 database ?Thank you for your reply. No, I must admit I didn't think of looking at the wait events. I will look at this as soon as I get a chance.
Thanks again for your suggestion. -
Store SQL Query in Oracle database
Hello,
I am storing SQL query in database. here is an example of query.
INSERT INTO TABLE_1 Values (' " + getValue1() + " ', ' " + getValue2() + " ' )
getValue1() and getValue2() are functions to retrieve values.
When I read query from the database and execute it, it doesn't read values from function.
After I read query from database I would expect it to be
INSERT INTO TABLE_1 Values ('value1', ' value2' ) but it is not reading values and trying to execute whatever I got from database.
Any help?
ThanksThanks for your reply. Let me give you more info.
All I am trying to do is storing queries in oracle
database to use it in future for reporting. As I am
doing reporting where clause will be different every
time. Reporting involves queries not updates. But in any case that means that you would use a java SQL string that is useable via a prepared statement. As such your statement would look like....
INSERT INTO TABLE_1 Values (?,?)
Is it not possible to substitute variable string I
got from database?What do you mean?
SQL is SQL. You construct SQL so it is valid and runs. Can you construct SQL so it runs a function/select and uses that value in some more SQL - depends on the database, but usually.
But that has nothing to do with java - it is SQL.
Query contains variables that I have in Java code .Then it is SQL. It is java. You use bind variables (see PreparedStatement) and assign values to it.
The SQL for such a query would look like the following...
select field1, field2 from table1 where id=?
In your java you would then use something like PreparedStatement.setInt(1, 100), to fill in the value for the '?' in the above.
You might also note that your solution will not work for any arbritrary SQL statement unless you are also storing meta data about the SQL itself. For instance the following SQL statements would return the same result set but your java code would have to populate the bind variables different for both...
select field1, field2 from table1 where id=? and name=?
select field1, field2 from table1 where name=? and id=? -
Hello, i'm from Colombia, I don't speak inglish, but i have a question.
i try make a query from my database, i have the mysql query but i don't know how is the JPQL Query.
Mysql
select distinct (i.codigo), count(ia.incidente) from incidente i left join incidentearchivo ia on (i.codigo = ia.incidente and ia.activo = true)
where i.tipo = 'I'
group by i.codigo
order by i.consecutivo;
i have this query and run fine in mysql but i need this query in JPQL to my application.
very tanks for your help.The JPQL depends on your object model, so perhaps include the classes and their mappings.
Assuming a Incidente and IncidenteArchivo class with a OneToMany it may be something like:
Select distinct (i.codigo), count(ia.incidente) from Incidente i left join i.archivos ia
where i.tipo = 'I' and ia.activo = true group by i.codigo order by i.consecutivo
The main difference is that JPQL does not have an ON clause, joins are defined by the relationships. This could be an issue with the activo = true check.
You can also use a native SQL query in JPA to use your existing SQL.
James: http://www.eclipselink.org -
How to get Hierarchical XML File from a Database Join Query !
Hi,
How can i get a Hierarchical XML File from a Database Join Query ?
Any join query returns repeated values as below:
BD17:SQL>select d.dname, e.ename, e.sal
2 from dept d
3 natural join
4 emp e
5 /
DNAME ENAME SAL
ACCOUNTING CLARK 2450
ACCOUNTING KING 5000
ACCOUNTING MILLER 1300
RESEARCH SMITH 800
RESEARCH ADAMS 1100
RESEARCH FORD 3000
RESEARCH SCOTT 3000
RESEARCH JONES 2975
SALES ALLEN 1600
SALES BLAKE 2850
SALES MARTIN 1250
SALES JAMES 950
SALES TURNER 1500
SALES WARD 1250
14 rows selected.
We tried use DBMS_XMLQUERY to generate a xml file, but it was unable to get xml in Hierarchical format.
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <ROWSET>
- <ROW num="1">
<DNAME>ACCOUNTING</DNAME>
<ENAME>CLARK</ENAME>
<SAL>2450</SAL>
</ROW>
- <ROW num="2">
<DNAME>ACCOUNTING</DNAME>
<ENAME>KING</ENAME>
<SAL>5000</SAL>
</ROW>
- <ROW num="3">
<DNAME>ACCOUNTING</DNAME>
<ENAME>MILLER</ENAME>
<SAL>1300</SAL>
</ROW>
- <ROW num="4">
<DNAME>RESEARCH</DNAME>
<ENAME>SMITH</ENAME>
<SAL>800</SAL>
</ROW>
- <ROW num="5">
<DNAME>RESEARCH</DNAME>
<ENAME>ADAMS</ENAME>
<SAL>1100</SAL>
</ROW>
- <ROW num="6">
<DNAME>RESEARCH</DNAME>
<ENAME>FORD</ENAME>
<SAL>3000</SAL>
</ROW>
- <ROW num="7">
<DNAME>RESEARCH</DNAME>
<ENAME>SCOTT</ENAME>
<SAL>3000</SAL>
</ROW>
- <ROW num="8">
<DNAME>RESEARCH</DNAME>
<ENAME>JONES</ENAME>
<SAL>2975</SAL>
</ROW>
- <ROW num="9">
<DNAME>SALES</DNAME>
<ENAME>ALLEN</ENAME>
<SAL>1600</SAL>
</ROW>
- <ROW num="10">
<DNAME>SALES</DNAME>
<ENAME>BLAKE</ENAME>
<SAL>2850</SAL>
</ROW>
- <ROW num="11">
<DNAME>SALES</DNAME>
<ENAME>MARTIN</ENAME>
<SAL>1250</SAL>
</ROW>
- <ROW num="12">
<DNAME>SALES</DNAME>
<ENAME>JAMES</ENAME>
<SAL>950</SAL>
</ROW>
- <ROW num="13">
<DNAME>SALES</DNAME>
<ENAME>TURNER</ENAME>
<SAL>1500</SAL>
</ROW>
- <ROW num="14">
<DNAME>SALES</DNAME>
<ENAME>WARD</ENAME>
<SAL>1250</SAL>
</ROW>
</ROWSET>
Thank you for some help.
Nelson AlbertiHi,
I wrote a general ABAP program which can be configured to grab contrent from an URL and post that content as a new PI message into the integration adapter .... from that point on normal PI configuration can be used to route it to anywhere ...
It can be easily scheduled as a background job to grab content on a daily basis etc ...
Regards,
Steven -
How to generate XML file from oracle database query result
Hi dudes,
as stated on the subject, can anyone suggests me how can i achieve the task stated above??
Here is a brief description of my problem:
I need to create a XML file once i query from the oracle database, and the query result returned from the database will be stored in XML file.
I'd searched around the JAXB, DOM, SAXP and the like basic concepts, but i still don't know how to start??
Any suggestions ???Read this:
http://www.cafeconleche.org/books/xmljava/chapters/ch08s05.html
You might have to read more of the book to understand that chapter. -
Select query based on joining of tables from different database is taking too long
Hi Team,
Select query on table with millions of records is taking very long time. It took 50mins, the below query is joining on multiple table from two databases DB1.dbo.Table1 contains 100 million records and also Table3 and Table4 (of different database) might
contain close to 1million records.
Select T1.*
From DB1.dbo.Table1 T1
Join DB1.dbo.Table2 T2 on RTRIM(T1.Col3) = RTRIM(T2.Col3)
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
Join Table3 U on CA.Col2 = U.Col2 AND RTRIM(T2.Col2) = U.Col3
Where U.Col4 NOT IN ('A1', 'A2', 'A3', 'A4', 'A5', 'A6','A7','A8','A9')
And (T1.flg IS NULL OR T1.flg = 'N')
And LTRIM(RTRIM(T2.Col2)) NOT IN ('B1','B2')
How can i improve the performance of this query. Actual thing is update the data in Db1.dbo.table1 based on the conditions but if the select is taking close to 1hr then update will take hours together. Indexes already implemented on all the tables.
Thanks,
Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.Thanks.
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
This join is not working with spaces if Rtrim is not used.
Thanks! Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users. -
Trying to display results from access database query on a JSP
Seem to be having real difficulty with this one, i keep getting a null pointer exception :(. Wondered if anyone could point me to any examples of displaying the results on jsp. The database is connected, as i can display the query on netbeans, but just not on a jsp.
I think it would be good if we had a section in these forums for pre-canned answers to the same questions that come up again and again. It would save time.
Here is a rough outline for using JSP:
* Read a JSP book.
* The JSP page (View in MVC) should get all the data it needs to display from a useBean and/or request.getAttribute().
* The JSP page should not have any business logic. Its purpose is to display data only (and have some submit buttons such as 'update').
You can do some basic client side validation using javascript if you want. Because there is no business logic in it, you can easily debug
your code in the servlet or business logic. You can't set breakpoints in JSP and examine data easily.
* When you click the submit button, all data from <input type="text" tags, etc is put into request scope via name/value pairs.
* Submit the JSP page <form tag to a servlet (Control in MVC).
* The servlet reads the name/value pairs of data from the JSP page via request.getParameter()
* The servlet should then instansiate a business object (Model in MVC), passing the data it got from the page to the business logic.
Example: ProcessPage processPage();
if(request.getParameter("updateButtonClicked"))
processPage.updateData(data from the jsp page)
* The business logic should instansiate a separate database object (DAO) to get data to/from the database. Business logic should not have sql in it.
Business logic should not generate html. Business logic should not have request objects in it.
* The business logic should return data to the servlet that in turn will put it in request scope via request.setAttribute() so the JSP page can draw itself.
* The servlet should then call up the approprate JSP page. -
Single query to get data from different databases
i need to capture certain fields from certain tables in database 1 and certain fields from certain tables in database 2 into one file using a single SQL statement.
i tried searching on the net
i found that dblinks can help
but iam not sure if ill be able to create dblinks in my situation which is:
i need to get data from oracle to be copied to mysql
this is not a replication acitivity, but i need certain fields from one database and certain from the other
so what iwas thinking is, if i use an sql query to get all the fields (i need around 40) from the different oracle databases and create a singlefile with one insert per select, i can then read that file into mysql
instead of creating multiple sql queries for each table and creating separate files and eventually separate tables in mysql.
can anyone help me here?
or maybe suggest another approach.
thanksHi,
I think dblink is the only option available to get data from different databases. It will work for your case too.
CREATE DATABASE LINK db_link CONNECT TO user_name IDENTIFIED BY password USING 'instance_name'you must have the system privilege 'create database link' to create db links. This way you can get the required data and put it in a table in oracle. But i dont know how to put this data from oracle table to Mysql.
HTH
Muneer -
Select query from 2 schema & database
Hi,
what privileges should i give to execute select query from 2 schema & database in oracle
thanksHi,
not so clear, if you are looking for this
-- for other schema access
GRANT SELECT ON <scheman_name>.<table_name> TO <schema_name>;
or create the synonym
--for other database access you need to create db link -
I'm writing a Java program that needs to query a Microsoft Access database. I'm not sure how to do this. Someone said to use ODBC drivers but I don't know what these are. Any clarification/ideas would be appreciated.
There are a few easy step to set up a connection to your database in your java program.
1. establish an odbc data source in windows control panel (may be under Administrative tools).
2. you need to import the java.sql package.
3. you need a connection, so type the following into your database class:
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:/*your data source name goes here*/");
catch (ClassNotFoundException exception)
System.err.println("Failed to load JDBC/ODBC driver");
exception.printStackTrace();
System.exit(1);
catch (SQLException exception)
System.err.println("Unable to connect");
exception.printStackTrace();
System.exit(1);
}4. Find an SQL tutorial on the web.
5. For each query/update etc create a Statement object using the following code:
try
Statement statement = connection.createStatement();
/* now use the remainder of this try block to execute your query. Each database query returns a ResultSet object */
ResultSet resultSet = statement.executeQuery("/*your query string goes here*/");
/* the next() method in ResultSet returns true if there are any more records, like an iterator. Therefore, you know if there are any results or not*/
if (resultSet.next())
/* If your query was a 'SELECT * FROM ...etc' then you will get a whole row from the table(s) within the database. if you need to extract a number from that record and you know the column number, then use the following */
int id = resultSet.getInt(1);
/* where 1 corresponds to the first column in the table, and so on. Ifyou want to extract a String, the use the getString() method*/
catch (SQLException exception)
}That's basically the easiest way to go about it (I think). Best to consult a book for a more in-depth look. Try Java - How to Program, by Deitel and Deitel (available at www.deitel.com). I found this very useful.
Maybe you are looking for
-
Can u print without AirPrint printer if you have a wireless printer?
Can u print without AirPrint printer if you have a wireless printer? I have a fairly new canon printer, MP495 series all in one, and was wondering if there is anyway to print wirelessly to it or if I have to buy another printer that is airprint compa
-
hi there if i associate my apple id with another email will i lose my icloud data or i will still be able to access all my stored data with the new linked to my apple id email address? i mean i am not signing up for a new account, just changing my ol
-
Update data of pdf (offline adobe forms) to sap system By Inbound E-mail
Hello, I am trying to update data in PDF (offline adobe forms) to Sap system by Inbound E-mail. Kindly give some suggestions for scenario to do this task.
-
<blockquote>Locking duplicate thread.<br> Please continue here: [/questions/793396]</blockquote><br> cannot use search box, because I cannot type in the search area. This has never happened before. This is a big problem can you help?
-
Why is the First Item Moved by a Rule is Grayed Out
Howdy, When rules are applied to move e-mails from my Inbox to some other folder the first item in any of those folders is either grayed out and not listed as new or has the Junk Mail Flag set even though Mail doesn't think it's junk mail (i.e., the