java db2_java访问db2表代码

public class ConnToDb2 {

//获得数据库连接

public static Connection getconn(String location,String username,String password)

{

Connection conn = null;

try {

Class.forName("com.ibm.db2.jcc.DB2Driver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

conn = DriverManager.getConnection(location,username,password);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

//查询数据

public static void select()throws Exception

{

Connection conn = getconn("jdbc:db2://192.168.10.100:50000/yan", "db2inst1", "abc");

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select *from test");

System.out.println("表中的数据如下:");

while (rs.next())

{

System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));

}

stmt.close();

conn.close();

}

//插入数据

public static void insert()throws Exception

{

System.out.println("插入数据....");

Connection conn = getconn("jdbc:db2://192.168.10.100:50000/yan", "db2inst1", "abc");

Statement stmt = conn.createStatement();

int rs = stmt.executeUpdate("insert into test values (3,'li','si')");

stmt.close();

conn.close();

System.out.println();

}

//跟新数据

public static void update()throws Exception

{

System.out.println("更新数据....");

Connection conn = getconn("jdbc:db2://192.168.10.100:50000/yan", "db2inst1", "abc");

Statement stmt = conn.createStatement();

int rs = stmt.executeUpdate("update test set password='kkk' where name='li'");

stmt.close();

conn.close();

System.out.println();

}

//删除数据

public static void delete()throws Exception

{

System.out.println("删除数据....");

Connection conn = getconn("jdbc:db2://192.168.10.100:50000/yan", "db2inst1", "abc");

Statement stmt = conn.createStatement();

int rs = stmt.executeUpdate("delete from test where name='li'");

System.out.println();

stmt.close();

conn.close();

}

public static void main(String[] args)throws Exception {

ConnToDb2 con = new ConnToDb2();

con.select();

con.insert();

con.select();

con.update();

con.select();

con.delete();

con.select();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值