• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

一个jdbc 测试程序代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

复制代码 代码如下:

import java.sql.Date;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
public class FirstOracleJdbc {
public static void main(String[] args) throws SQLException {
insert(new int[]{5},new String[]{"iGoder"}, new Date[]{Date.valueOf("1980-01-01")});
select();
}
public static void select() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "test", "test");
st = con.createStatement();
String sql = "select * from student";
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.print("id=" + rs.getInt("id"));
System.out.print(",name=" + rs.getString("name"));
System.out.print(",birthday=" + rs.getDate("birthday")+"\n");
}
System.out.print("+++++++++++++++++++++++++++++++++++");
//printRS(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (Exception e) {
}
try {
st.close();
} catch (Exception e) {
}
try {
con.close();
} catch (Exception e) {
}
}
}
public static void insert(int[] ids, String[] names, Date[] dates)
throws SQLException {
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "test", "test");
con.setAutoCommit(false);
String sql = "insert into student(id, name, birthday) values(?,?,?)";
ps = con.prepareStatement(sql);
for (int i = 0; i < ids.length; i++) {
int index = 1;
ps.setInt(index++, ids[i]);
ps.setString(index++, names[i]);
ps.setDate(index++, dates[i]);
ps.executeUpdate();
}
con.commit();
} catch (Exception e) {
e.printStackTrace();
try {
con.rollback();
} catch (Exception e1) {
}
throw new SQLException(e.getMessage());
} finally {
try {
ps.close();
} catch (Exception e) {
}
try {
con.close();
} catch (Exception e) {
}
}
}
public static void printRS(ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
while(rs.next())
{
for(int i = 1; i <= colCount; i++)
{
if(i > 1)
{
System.out.print(",");
}
String name = rsmd.getColumnName(i);
String value = rs.getString(i);
System.out.print(name + "=" + value);
}
System.out.println();
}
}
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
jsp struts1 标签实例详解第1/2页发布时间:2022-02-05
下一篇:
SSH整合中 hibernate托管给Spring得到SessionFactory发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap