COLUMN_NAME loop Oracle Database - java

I have some problems with my code. I want to extract the column names from my table. Everything works fine in mySQLWorkbench and it shows me all of my column names.
For example the output in mySQL Workbench is : nr,height,length
The output on my oracle database is: nr,height,length,nr,height,length,...
it looks like I'm in a loop, but I cant understand why. The oracle database is located at my university, so I can't get any access to work with it like mySQLWorkbench.
I tried this code below and at the moment i really dont know what to do.
ResultSet columns = databaseMetaData.getColumns(null,null, tableName, null);
while(columns.next())
{
String columnName = columns.getString("COLUMN_NAME");
System.out.println(columnName + ";");
}
in my opinion the output should be: nr;height;length

Related

Why will Ucanaccess not delete my records?

I can connect to my access database and select, insert records etc. I am now trying to delete records and as far as I can see I am using the correct syntax. I have followed just about every tutorial I can find and they are not doing anything different that I can see.
String deleteSql = "DELETE FROM table1 WHERE sometext=? or sometext=?";
ps = module.getSupportConnection().prepareStatement(deleteSql);
ps.setString(1,"four");
ps.setString(2,"five");
int rs = ps.executeUpdate();
System.out.println(rs);
I have tried it without using int rs = .. but I used it just to see what the output was and it returns '2' which is what I was expecting as there are two records that meet the criteria used. It just wont delete the records and I cant see why. I dont get any errors when running the code. I appreciate this may not be a ucanaccess issue per se.

How do I add a new row of data into my Derby database?

Using Netbeans, I have my database and table set up, and have added my data manually, in which I am able to see within my application I am building, as intended.
I would like the user to add their own data in which will be appended to a new row on the table. However, I am having trouble trying to write code in order to do this.
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/stockApplication");
Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String insertDerbyData = "INSERT INTO TIGER_INFO"
+ "(TIGER_ID, TIGER_LOCATION)"
+ "VALUES (123456, Store)";
stat.executeUpdate(insertDerbyData);
I cannot execute the above code as I'm returned with an error mentioning that 'STORE' is not in any table. 'STORE' is meant to be a value for my 'TIGER_LOCATION' column. What's going on here?
In theory, I have two columns, and I would like to add both values, '123456' and 'Store' into their respective columns. How do I go about correctly doing so?
If TIGER_LOCATION is a string/varchar column, and Store is a string literal, then the value must be enclosed in single quotes, as in most SQL-based databases:
INSERT INTO TIGER_INFO (TIGER_ID, TIGER_LOCATION) VALUES (123456, 'Store')
Strings should be between '...' you have to use :
VALUES (123456, 'Store')
//--------------^-----^

getting all the data from mysql database table is not working in java

i am trying to get all the data from a certain table.but it's only giving me only the first row twice a result (as i have two rows in the database)
here is my code
String data[]=new String[10];
String[] result;
Product p= new Product();
int serial=0;
try{
String sql="select * from product";
rslt=st.executeQuery(sql); //where private static Statement st, private static ResultSet rslt;
while(rslt.next()){
data[1]=rslt.getString("p_code");
data[2]=rslt.getString("p_name");
/* data[3]=rslt.getString("description");
data[4]=rslt.getString("measurement");
data[5]=Integer.toString(p.RemainProduct(data[1]));
data[6]=p.getSellPrice(data[1]);
serial+=1;
data[0]=Integer.toString(serial);
DTB.addRow(data); */
System.out.println("code :"+data[1]+" "+"Name :"+data[2]);
}
}catch(Exception ex){
System.out.println("ERROR :"+ex);
}
my table has two data, here is my database table data
and here is the result after i run the program.
i don't know where is the actual problem. the same code works fine on the other method but why i can't get it here. i am very new to java, please help me fixing this problem
Your code looks correct to me. I think this is a case of "what you are looking at is not what is broken". Perhaps you are not querying the table you think you are querying, or perhaps your table view is cached and out of date. Refresh your table view, and try printing out the PK (id column in the table) in your program output as a double check that the table your are querying is the correct table. Also, double check your JDBC URL and verify you are querying the correct database.
Also, your comments indicate that the same code is giving correct results in a different method. Maybe there is more going on in your actual code that we don't have insight into? Perhaps your code (not shown in your example) is:
Updating the table so that row 1 and 2 have the same data.
Querying the database (as shown in your example), and returning same data in row 1 and 2.
Update the table so that row 1 and 2 now have different data which shows up when you manually query the database.
Finally, make sure some of your other team members or an automated test process isn't updating the table without your knowledge.

String Converting Issue

Ive been stunk in working with java and mysql these days.
The problem is, ive got a mysql database. There is a column in one table which shows the chinese city names. One collegue changed the db to utf8 for every character(connection, db, results, server and system) The consequence is that the data before the change didn't show correctly any more only if i set the %character% back to latin1. In either character set i can only retrive half the data correctly. Could you please help me how to solve the problem?
Ive tried to use java to solve the problem but it doesn't work.
String sql = "SELECT * FROM customer_addresses";
ResultSet result = query.executeQuery(sql);
while (result.next()) {
byte b[] = result.getBytes("city");
c = new String(result.getBytes("city"), "UTF-8");
}
For example: there is one city in db like this 乌é²æœ¨é½å¸‚
the java print: 乌�?木�?市
it should be:乌鲁木齐市
Thanks in advance
Default charset of your MySQL server is probably not UTF8. Try to execute the following SQL queries before getting data from the database:
SET NAMES utf8
and
SET CHARACTER SET utf8
Add characterEncoding=UTF-8 to the connection string, where you connect to the database. For example:
"jdbc:mysql://servername:3306/databasename?characterEncoding=UTF-8"
Incidentally, the data in the database appears to be broken. If you want the database to store 乌鲁木齐市, that's what should be in the table, not 乌é²æœ¨é½å¸.
Update: The problem with the how the data is stored in the database is easier to solve using database's own tools, not Java. For each table that stores text do this:
ALTER TABLE tablename CONVERT TO CHARACTER SET binary;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8;

"ResultSet is from UPDATE: No Data" received from Java application

I am trying to use a Java application (which I do not have the source code for) to output the results of a call to a stored procedure into a text file.
This file works for other similar stored procedures in the system, but I can't seem to get it to produce anything for my new text file other than this exception:
ResultSet is from UPDATE: No Data
I've simplified the body of the stored procedure to a simple select 'Hello World!' and even that doesn't seem to be able to be written out.
Is there anything I can do within the stored procedure to produce results in a fashion that Java will accept?
I encountered this java.sql.SQLException. In my case I was running a query in this way:
String query =
"-- a classical comment " +
"select * " +
"from MYTABLE ";
ResultSet rs = conMain.createStatement().executeQuery(query);
while(rs.next()) {
//do something...
}
rs.next() throws the exception. The reason is that, due to the comments, query results to be:
"-- a classical comment select * from MYTABLE "
hence it's all commented... query is invalid! Many examples could be shown with this mistake (with the comment in the middle of the query etc.).
Solutions: add a \n at the end of each line of the query or use comments in the /*...*/ form.
I selected an older version of the driver an it worked for me.
http://dev.mysql.com/downloads/mirror.php?id=13598 (mysql-connector-java-5.0.8.zip)

Categories

Resources