my table is:
[tableAbc]
A B C
------------
1 a b
2 c e
3 e r
...
run:
show create table tableAbc;
to get the create table sql
but how to get insert sql?
like:
insert into `tableAbc` ('A','B','C') VALUES(1,'a','b');
...
any idea? (or any java library to do this)
thanks all!
BTW:
i want show the "insert sql" to web brower.
so,i think i need get "insert sql" under java, or sql commands.
If you use Squirrel (A SQL tool) it can generate this for you.
I use this tool daily (it is free and it works well)
As per their description
SQuirreL SQL Client is a graphical
Java program that will allow you to
view the structure of a JDBC compliant
database, browse the data in tables,
issue SQL commands etc,
To get the insert statement for a table just right click on a table and select "data scripts"
Your question is difficult to understand due to everything being in sentence-fragments, but I think you're basically just looking for a way to be able to send SQL statements to your SQL database from Java, specifically an insert statement?
If this is the case, Java has an sql package. You can create a connection to your database, create a statement, and execute it. This will then return a ResultSet which is the result of the executed statement. Start here perhaps? http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html#createStatement%28%29 And there is part of an example on the documentation for ResultSet http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html
What do you mean by "i want to show the insert sql in the browser"? Do you mean you want to show what the result of the executed statement is?
Related
I have a very strange behavior when I execute e query from Java code with Hibernate and when I call the same query directly from SQL Developer.
In the first case the entry could not be found but in the second case it is found.
1st case (Java code query):
getEntityManager().createNativeQuery("SELECT MAX(VERSION) FROM TIME_VERSION WHERE DIRECTORY=1").getResultList();
returns null.
2nd case (SQL Developer query):
SELECT MAX(VERSION)
FROM TIME_VERSION
WHERE DIRECTORY=1;
returns a value.
Important: A more strange behavior, is that, if I will edit any column value
from this entry (ex. VERSION) with SQL Developer and then I try to execute again the
Java Query from inside the code, it works correctlly and returns the same result as the query from the SQL Developer.
Does anybody knows why happens such a behaviour and what could be done to avoid it?
P.S. TIME_VERSION table has 7 Columns and a custom primary key. The database is an Oracle DB.
As your query is returning a single result, can you try
getEntityManager().createNativeQuery(
"SELECT MAX(VERSION) FROM TIME_VERSION WHERE DIRECTORY=1").getSingleResult()
I did found the problem:
When I was adding this entry in the database I had not done a commit at the end. I don't know why if you make a search query inside SQL Developer it will be found (even without the commit) but if you make a search from some external environment (like Java Hibernate) it will not be found! Maybe there is a more professional answer.
Solution: I just made a commit; at the end (after the insert query) and now is functioning correctly.
Thank you anyway!
I am trying to find out a SQL query through which i can get to know if i am connected to database or not. I will be using JAVA program for executing this query. I am in search of a generic query that when executed from my java program can give me a result if i am connected to database. I am not aware of the database structure(tables in database), i just know database name, to which i am trying to connect.
I am in search of a generic SQL query which can work on any database irrespective of the underlying database such as SQL Server, MySql, Oracle or any other.
Till now i have got some queries but they are database specific they cannot be used with every database.
select count(*) as TablesCount from sys.tables :- this query is
SQL server specific.
select 1 :- works for every database but according to me this
is not a good aproach.
show tables :- i cannot use show tables as thiquery displays
the name of tables in the database
but i am just concerned whether i can access database or not.
question 1:- Please suggest if i can make a query that can execute irrespective of underlying database(I am not aware of database structure).
question 2:-
can i store result of query (query - show tables) in resultset.
I have a swing desktop application that is
connected to a derby DB.
I use native sql query to select all data from the table
Now I want to update the data in the table with native sql queries
but first I have to retrieve the input data from the swing textfields.
I am able to use the jtextfielld.gettext()
to retrieve that data but how do i set these textfields data to
the update query since the sql query is executed as a string statement.
for example :
String sql = "UPDATE APP.REGISTRY SET LETTER_FROM ='Japan Motors' WHERE id = 7"
so how do execute this query from the swing interface.
Please help.
Have you looked at PreparedStatements ?
Although PreparedStatement objects can be used for SQL statements with
no parameters, you probably use them most often for SQL statements
that take parameters. The advantage of using SQL statements that take
parameters is that you can use the same statement and supply it with
different values each time you execute it.
See the linked doc and note in particular the placeholder capability (using ?s) that allows you to safely insert parameters in your SQL.
It appears you are new to JDBC and looking for a way to execute queries form Swing App. If this is a case, I will refer you to look at JDBC documentation.
In essence, you will need to get a reference to Connection object, then create a PreparedStatement using the connection and then then you can call executeUpdate() method followed by commit() method.
However, Please understand that you have very limited options for getting connection in a swing app. You will need to look at the usage of DriverManager for the way to get connection for a database.
I used PreparedStatement in my Java program. I need to debug the SQL query because it's not working fine.
Is there is away to print the used SQL statement with the values inserted
for example in PreparedStatement:
select * from table where a=?
and than I set the ?
could I print the used SQL, for example:
select * from table where a=1
It can't be done with java.sql.PreparedStatement interface itself; it depends on the implementation by your database vendor.
But you're in luck; the MySQL driver allows you to do it using its toString implementation:
http://www.avajava.com/tutorials/lessons/how-do-i-display-a-prepared-statement-with-bind-variables-using-mysql.html
You need to be aware that using this vendor-specific feature binds your code to MySQL. You can't change databases without changing your code.
I'm using Java to connect to an Oracle 10 Database.
I want to create a stored procedure (don't ask why) that takes no arguments
and returns a lot of rows. Specifically, in Java I want to be able to get this
data with something like:
ResultSet rs = stmt.executeQuery("call getChildless");
where getChildless is the query:
SELECT objectid
FROM Object
WHERE objectid NOT IN (SELECT parent FROM subparts);
However, I just cannot for the life of me figure out how to get my
output from the stored procedure. I've googled it and I get
all this sample code that Oracle won't compile, presumably it's for a
previous version. Refcursors seem to come up a lot, but I'm not sure
if that's what I actually want, to use it with a ResultSet.
I presume Oracle JDBC is like MS-SQL JDBC...
while (rs.next())
{
myint = rs.getInt(1);
}
JDBC is dynamically typed, It does not make sense that it would not compile.
See this link to the JDBC Tutorial