oracle改连接数据库,oracle连接数据库增删改查

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值