📄 simplesql.java
字号:
//声明本类包含在包examples.jdbc.oracle中
package examples.jdbc.oracle;
//声明本类要引入的其他包和类
import java.sql.*;
import java.util.Properties;
/**
* 这个实例演示怎样使用JDBC执行DDL和DML。
*/
public class simplesql {
public static void main(String argv[])
{
//声明连接,SQL语句变量
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
try {
//设置属性变量:用户、密码和服务器
Properties props = new Properties();
props.put("user","scott");
props.put("password","tiger");
props.put("server","DEMO");
//加载数据库驱动程序
Driver myDriver = (Driver) Class.forName("weblogic.jdbc.oci.Driver").newInstance();
//创建连接
conn = myDriver.connect("jdbc:weblogic:oracle", props);
//创建SQL语句对象
stmt = conn.createStatement();
try {
//执行SQL语句,删除表empdemo
stmt.execute("drop table empdemo");
System.out.println("Table empdemo dropped.");
}
catch (SQLException e) {
//异常处理
System.out.println("Table empdemo doesn't need to be dropped.");
}
//执行SQL语句,创建表empdemo
stmt.execute("create table empdemo (empid integer, name varchar2(30), dept number(4))");
System.out.println("Table empdemo created.");
//执行SQL语句,在表empdemo中插入一条记录
int numrows = stmt.executeUpdate("insert into empdemo values (0, 'John Smith', 12)");
System.out.println("Number of rows inserted = " + numrows);
//执行SQL语句,删除empid = 0的记录
numrows = stmt.executeUpdate("delete from empdemo where empid = 0");
System.out.println("Number of rows deleted = " + numrows);
} catch (Exception e) {
//异常处理
System.out.println("SQLException was thrown: " + e.getMessage());
} finally { //关闭连接和语句对象
try {
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
} catch (SQLException sqle) {
//关闭异常
System.out.println("SQLException was thrown: " + sqle.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -