How to execute SQL Server query into JDBC like this SQL?
select * into bk_table from existing_table
Expected: Above query should be executed through JDBC.
Actual: Query is not working through JDBC in any way.I tried following two ways but nothing happens or affected in DBMS.
String sql="select * into bk_table from existing_table";
//tried way 1-> result: false
statement.execute(sql);
//tried way 2-> result: -1
statement.executeUpdate(sql);
What to do now?
You need to use an insert-select statement. It can be called with executeUpdate:
String sql = "INSERT INTO bk_table SELECT * FROM existing_table";
statement.executeUpdate(sql);
To select data from old table, "SELECT * FROM bk_table";
To insert the selected data from old to existing table, "INSERT INTO existing_table SELECT * FROM bk_table";
Related
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn=DriverManager.getConnection(URL,username,password);
String sql="select * from test where user_id='abc'";
stmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE);
ResultSet rs=stmt.executeQuery();
rs.afterLast();
getting exception in this line, afterLast operation not allowed .
Reading from the oracle documentation:-
To produce a scroll-sensitive result set: A query cannot use SELECT * .
However, there is a workaround for this.
As a workaround for the SELECT * limitation, you can use table aliases, as shown in the following example:
SELECT t.* FROM TABLE t ...
Change your query to select test.* from test where user_id='abc'
or use specific column names to retrive instead of *.
This question already has answers here:
Execute "sp_msforeachdb" in a Java application
(3 answers)
Closed 1 year ago.
I am executing the following query from Microsoft SQL Server Studio, which works fine and displays results:
SELECT *
INTO #temp_table
FROM md_criteria_join
WHERE user_name = 'tecgaw'
UPDATE #temp_table
SET user_name = 'tec'
WHERE user_name != 'tec'
SELECT *
FROM md_criteria_join
WHERE user_name = 'tec'
AND view_name NOT IN (SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
UNION
SELECT *
FROM #temp_table
ORDER BY view_name,
user_name,
crit_usage_seq,
crit_join_seq
However, if I execute the same query in Java, an Exception is thrown stating
The statement did not return a result set.
Here's the Java code:
statement = conn.getConnection().createStatement();
resultSet = stmt.executeQuery(sql.toString());
Is that because I cannot do multiple SQL queries in one statement (I.e., Creating the #temp_table, updating it, and then using for it my select statement)?
JDBC is getting confused by row counts.
You need to use SET NOCOUNT ON.
Use execute statement for data manipulation like insert, update and delete and
executeQuery for data retrieval like select
I suggest you to separate your program into two statements one execute and one executeQuery.
If you do not wish to do that, try separating the statements with semi-colon. But I am not sure about this action if this gives you a resultset or not.
I have found similar question in StackOverflow here. You should enable connection to support multiple statements and separate them using ;. For concrete examples see that answer. However it is for MySql only.
Also I think you can rewrite your SQL into single query
SELECT columnA, columnB, 'tec' as user_name from md_criteria_join
WHERE (
user_name = 'tec'
AND view_name NOT IN (
SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
)
OR user_name = 'tecgaw'
ORDER BY view_name, user_name, crit_usage_seq, crit_join_seq
Another option is to move your statements to stored procedure and ivoke it from JDBC using CallableStatement
Or maybe you should try executing it with multiple jdbc statements like this
Connection conn = conn.getConnection(); //just to make sure its on single connection
conn.createStatement("SELECT INTO #temp_table").executeUpdate();
conn.createStatement("UPDATE #temp_table").executeUpdate();
conn.createStatement("SELECT ...").executeQuery();
Note you have to close resources and maybe for better performance you could use addBatch and executeBatch methods
in ms sql you also have to do set nocount on right at the beginning of the stored procedure along with terminating select / update/ insert block statement with ";"
Good evening
i have problem about a query of insert if not exist i have do it in another query and it worked
but now i have an array of class java Service
so i want test about the id : VLAN
because i execut every time and i want that not insert many time if it exist already on table sql
This is my query but they give me an error about it
for(Service srv:service){
srvDataLst.add(srv.getvlan());
PreparedStatement pst=conn.prepareStatement(
"INSERT INTO tout (VLAN,client,JR,vrf,address) "
+ "VALUES(?,?,?,?,?) SELECT DISTINCT "
+ "'"+srv.getvlan()+"' FROM dual WHERE NOT EXISTS("
+ "SELECT * FROM tout WHERE 'VLAN'='"+srv.getvlan()+"') ");
pst.setInt(1,srv.getvlan());
pst.setString(2,convertNullToEmptyString(srv.getdesc()));
pst.setString(3,convertNullToEmptyString(srv.getjr()));
pst.setString(4,convertNullToEmptyString(srv.getvrf()));
pst.setString(5,convertNullToEmptyString(srv.getaddress()));
pst.executeUpdate();
Thank you for help
The error is:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'SELECT DISTINCT '20' FROM dual WHERE NOT EXISTS(SELECT * FROM tout
WHERE 'VLAN'=' at line 1
It's either insert into ... values(...) or insert into ... select ..., you can't use both in the same statement.
I have successfully insert data with LOAD DATA INFILE. After that when I am using the query:
SELECT * FROM tempupload
WHERE columnName NOT IN (SELECT columnName FROM othertable)
Not giving me the desirable result. But when I convert columnName datatype to double (and after again to varchar) giving me desirable results.
Please guide me as I have to use these queries from my Java EE Application.
If you have any NULLs in your othertable you may have some issues with a NOT IN (see this). To improve your query you could try:
SELECT * FROM tempupload
WHERE EXISTS (SELECT 1
FROM othertable
WHERE othertable.columnName = tempupload.columnName)
I'm using JTDS as a driver to connect to SQL server.
Here's the query that's giving me problems:
SELECT EmpID,FirstName,LastName,CompanyName,DepartmentName,JobTitle,HireDate FROM Employees where UPPER(FirstName) LIKE 'KEVIN%'
It returns 2 rows on SQL Server. One that has 'KEVIN' in upper case and another that has 'Kevin' like so. I used the wildcard to make sure I get both results. In my EmployeeDAO class I'm using the following:
ps = con.prepareStatement("SELECT EmpID,FirstName,LastName,CompanyName,"
+ "DepartmentName,JobTitle,HireDate FROM Employees WHERE UPPER(FirstName) LIKE ?");
ps.setString(1, FirstName + "%");
rs = ps.executeQuery();
And then of course I put KEVIN on my main. It only returns ONE row, which is the 'Kevin' row.
How do I fix this so it returns all rows?
Your query looks fine (although I would uppercase the parameter value before setting it, to make it more robust). The problem is just in the way how you're collecting the rows from the ResultSet. Likely you're plain overriding the previous row with the next row so that you end up with only one row (the last one) in your collection.
Default collation of the SQL Server installation is SQL_Latin1_General_CP1_CI_AS and it is not case sensitive.
Change collation of the query:
SELECT Col1
FROM Table1
WHERE Col1 COLLATE Latin1_General_CS_AS LIKE 'KEVIN%'