📄 labl.java
字号:
package day01;
import java.sql.*;
public class Labl {
public static void main(String[] args) {
Connection con = null;
Statement stmts = null;
String sqls = null;
ResultSet rs = null;
try {
// 1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.获取数据库连接对象
con = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.1:1521:tarena", "openlab",
"open123");
stmts = con.createStatement();
sqls = "drop table xun";
stmts.executeUpdate(sqls);
sqls = "create table xun(id number(8) primary key,name varchar2(20) not null)";
stmts.executeUpdate(sqls);
sqls = "insert into xun values(8,'zhang')";
stmts.executeUpdate(sqls);
sqls = "insert into xun values(10,'li')";
stmts.executeUpdate(sqls);
sqls = "insert into xun values(6,'wang')";
int nums = stmts.executeUpdate(sqls);
System.out.println("影响了" + nums + "记录");
sqls = "select * from xun";
rs = stmts.executeQuery(sqls);
//System.out.println("ID" + " " + "Name");
StringBuffer sb = new StringBuffer();
sb.append(" 结果集\n");
while (rs.next()) {
// 第二种效率高//第一种要进行模式匹配
// System.out.print(rs.getInt("id")+" ");
// System.out.print(rs.getString("name"));
sb.append(rs.getInt(1) + " ");
sb.append(rs.getString(2) + "\n");
// System.out.print(rs.getInt(1)+" ");
// System.out.print(rs.getString(2));
// System.out.println("");
}
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ee) {
ee.printStackTrace();
}
}
if (stmts != null) {
try {
stmts.close();
} catch (SQLException eee) {
eee.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -