[英]Java Oracle jdbc SELECT statement
I am practising Oracle JDBC using java in Eclipse environment. 我在Eclipse环境中使用java练习Oracle JDBC。 I understood how to output SELECT * from product
by iterating each line of table using next()
. 我理解如何通过使用next()
迭代每一行表来SELECT * from product
输出SELECT * from product
。 I am stuggling To output the statement 我正在努力输出声明
SELECT pid, pname
from product
where price>20
here is my code: 这是我的代码:
import java.sql.*;
public class intro {
/**
* @param args
*/
public static void main(String[] args)
{
// throws SQLException
//initiazlie the connection
Connection con=null;
try //try connection to database
{
//load driver
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Oracle JDBC driver loaded ok.");
con=DriverManager.getConnection("jdbc:oracle:thin:test/123321@localhost:1521:orcl");
System.out.println("Connect with @oracle:1521:orcl");
//declaring statement
Statement stmt = con.createStatement();
String dropProductTable="drop table product cascade constraints";
//create string
String createProductTable="CREATE TABLE product(" +
"pid number," +
"pname CHAR(20)," +
"price number," +
"PRIMARY KEY (pid)" +
")"; //do not add the semicolon(;) after closing the parenthesis.
/*drop table */
stmt.executeUpdate(dropProductTable);
//execute the create statement
stmt.executeUpdate(createProductTable);//execure the create statement
//create string that holds the insert statement
String insertIntoProduct="INSERT INTO product VALUES (1,'Pepsi',10)";
String insertIntoProduct1="INSERT INTO product VALUES (2,'Fanta',20)";
String insertIntoProduct2="INSERT INTO product VALUES (3,'Mirinda',30)";
String insertIntoProduct3="INSERT INTO product VALUES (4,'Gum',5)";
String updatePrice="UPDATE product set price=55 where price=20";
//stmt.executeUpdate(insertIntoProduct);
stmt.executeUpdate(insertIntoProduct);
stmt.executeUpdate(insertIntoProduct1);
stmt.executeUpdate(insertIntoProduct2);
stmt.executeUpdate(insertIntoProduct3);
//update statement
stmt.executeUpdate(updatePrice);
//save the select statement in a string
String selectStat="SELECT * FROM product";
String selectProduct="SELECT pid, pname from product where price>20";
//stmt.executeUpdate(selectStat);
//create a result set
ResultSet rows = stmt.executeQuery(selectStat);
ResultSet rows1= stmt.executeQuery(selectProduct);
//stmt.executeQuery(selectStat);
int count=0;
while (rows.next()) {
count+=1;
String productNumber = rows.getString("pid");
String productName = rows.getString("pname");
String productPrice = rows.getString("price");
System.out.println("Row #:"+count);
System.out.println("Product#: "+productNumber);
System.out.println("Product Name: "+productName);
System.out.println("Price: "+productPrice);
}
int count1=0;
while (rows1.next()) {
count1+=1;
String productNumber = rows1.getString("pid");
String productName = rows1.getString("pname");
String productPrice = rows1.getString("price");
System.out.println("Row #:"+count);
System.out.println("Product#: "+productNumber);
System.out.println("Product Name: "+productName);
System.out.println("Price: "+productPrice);
}
con.close();
}
catch (Exception e)
{
System.err.println("Exception:"+e.getMessage());
}
}
}
when I am trying to ouput selectProduct
variable i got this error 当我试图输出selectProduct
变量时,我得到了这个错误
Exception:Invalid column name
Please need assistance 请需要帮助
here is the output i am getting 这是我得到的输出
Oracle JDBC driver loaded ok.
Connect with @oracle:1521:orcl
Row #:0
Product#: 2
Product Name: Fanta
Price: 55
Row #:0
Product#: 3
Product Name: Mirinda
Price: 30
In your SELECT you are only getting "pid" and "pname": 在您的SELECT中,您只获得“pid”和“pname”:
String selectProduct="SELECT pid, pname from product...
But then you're trying to use a field that isn't in your SELECT: 但是你要尝试使用SELECT中没有的字段:
String productPrice = rows1.getString("price");
Try putting "price" in your SELECT clause. 尝试在SELECT子句中加入“price”。
You have to replace 你必须更换
SELECT pid, pname from product where price>20;
with 同
SELECT pid, pname, price from product where price>20;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.