package i;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class connect {
private final static String URL = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
static final private String USERNAME = "scott";
private final static String PSW = "tiger";
public static void add() {
Connection connection = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(URL,USERNAME,PSW);
String sql = "insert into student(sno,sname,sage,saddress) values(?,?,?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, 8);
pstmt.setString(2,"鲁班");
pstmt.setInt(3,6);
pstmt.setString(4,"天美工作室");
int count=pstmt.executeUpdate();
if(count>0)
System.out.println("添加成功!");
else
System.out.println("添加失败");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("添加失败");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("添加失败");
}catch (Exception e) {
e.printStackTrace();
System.out.println("添加失败");
}finally {
try {
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void delete() {
Connection connection = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(URL,USERNAME,PSW);
String sql = "delete from student where sno=? ";
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, 8);
int count=pstmt.executeUpdate();
if(count>0)
System.out.println("删除成功!");
else
System.out.println("删除失败");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void update() {
Connection connection = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(URL,USERNAME,PSW);
String sql = "update student set sno=?,sname=?,sage=?,saddress=? where sno=?";
pstmt = connection.prepareStatement(sql);
//修改后的内容
pstmt.setInt(1,100);
pstmt.setString(2, "项羽");
pstmt.setInt(3,1000);
pstmt.setString(4,"楚国");
//要修改的人
pstmt.setInt(5, 7);
int count=pstmt.executeUpdate();
if(count>0)
System.out.println("修改成功!");
else
System.out.println("修改失败");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void query() {
Connection connection = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(URL,USERNAME,PSW);
String sql = "select * from student order by sno";
pstmt = connection.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
//处理结果集
while(rs.next()) {
int sno = rs.getInt("sno");
int sage = rs.getInt("sage");
String sname = rs.getString("sname");
String saddress = rs.getString("saddress");
System.out.println(sno+"-"+sname+sage+saddress);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// add();
//delete();
//update();
query();
}
}

被折叠的 条评论
为什么被折叠?



