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
