您的位置:首页 > 数据库 > Oracle

JSP连接Oracle小程序(创建数据表,插入,显示)

2004-10-13 13:19 447 查看
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<%
java.sql.Connection lConn = null;
java.sql.Statement lStat = null;
java.sql.ResultSet lRs = null;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String lUrl = "java:oracle:thin:@192.168.0.28:1521:junzi";
//java:oracle:thin: 表示使用的是thin驱动
//@srv:1521: 表示使用的服务器的名字和端口号
//dbname: 表示数据库的SID
lConn = DriverManager.getConnection(lUrl,"gu","jian");
lStat = lConn.createStatement();

//创建表
String createTableCoffees = "CREATE TABLE COFFEES " +
"(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " +
"SALES INTEGER, TOTAL INTEGER)";
lStat.executeUpdate(createTableCoffees);

//插入数据
lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Colombian', 101, 7.99, 0, 0)");
lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Espresso', 150, 9.99, 0, 0)");
lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)");
lStat.executeUpdate("INSERT INTO COFFEES VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)");

//查询结果
lRs = lStat.executeQuery("select * from COFFEES");

//显示结果
out.println("<table>");

while (lRs.next()) {
out.print("<tr><td>" + lRs.getString(1));
//COF_NAME
out.print( "<td>" + lRs.getInt(2));
//SUP_ID
out.print( "<td>" + lRs.getFloat(3));
//PRICE
out.print( "<td>" + lRs.getInt(4));
//SALES
out.println( "<td>" + lRs.getInt(5));
//TOTAL
}

out.println("</table>");

lRs.close();
lStat.close();
} catch (SQLException e) {
throw new ServletException(e);
} finally {
try {
if (lConn != null)
lConn.close();
} catch (SQLException e) {
}
}
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐