手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle与MySQL在jdbc中的区别

[日期:2015-12-23] 来源:Linux社区  作者:Linux [字体:大 中 小]

Oracle与MySQL在jdbc中的区别

①链接oracle的实例
【导包省略】
public class jdbcbak {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try {
//1.加载数据驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.取得链接 conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.3.23:1521:ORCL","student","123");
  //3.创建statement
 stmt=conn.createStatement();
//4.执行sql语句
 rs=stmt.executeQuery("select*from student");
 int amount=stmt.executeUpdate("delete from student where studentid=1");
 System.out.println("amount:"+amount);
 //5. 处理结果集
while(rs.next()){
System.out.println(rs.getString("studentname"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//6.资源释放
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

②MySQL实例
public class MySqlJDBC {
    public static void main(String args[]) {
        try {
            // 1.加载MYSQL JDBC驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Success loading Mysql Driver!");
        } catch (Exception e) {
            System.out.print("Error loading Mysql Driver!");
            e.printStackTrace();
        }
        try {
        //2.取得链接
            Connection connect = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "123456");
      // 连接URL为 jdbc:mysql//服务器地址/数据库名 // 后面的2个参数分别是登陆用户名和密码
            System.out.println("Success connect Mysql server!");
            //3.创建statement
            Statement stmt = connect.createStatement();
            //4.执行SQL语句
            ResultSet rs = stmt.executeQuery("select * from userinfo");
            //5.处理结果集
            while (rs.next()) {
                System.out.println(rs.getString("username"));
            }
        } catch (Exception e) {
            System.out.print("get data error!");
            e.printStackTrace();
        }
    }
}

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126698.htm

linux
相关资讯       jdbc 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款