JDBC-ODBC连接Oracle数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Heavy_Dream/article/details/78565453

工程目录:



lib放jdbc的jar包

工具类Util:

package com.jdbc;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;


public class ConnectionUtil {
public static Connection getConn() throws ClassNotFoundException, SQLException{
//1、加载Oracle的驱动类
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");

//2、由驱动管理类DriverManager去获得连接的对象Connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbhbd", "c##hang", "oracle123");
return conn;
}

public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = getConn();
//获得数据库的元数据信息,为了方便或者数据库的相关信息,比如名字或者版本号
DatabaseMetaData dbmd = conn.getMetaData();
System.out.println("恭喜你登录的是" +dbmd.getDatabaseProductName() + 
"名字的是数据库,版本号是" + dbmd.getDatabaseProductVersion());
}
}


操作类:

package com.jdbc;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import com.jdbc.bean.User;


public class OperationDB {

/**
* 增删改操作
* @param conn
* @param sql
* @throws SQLException
*/
public static void updateDB(Connection conn, String sql) throws SQLException {
//获得执行sql语句的对象。Statement对象或者PreparedStatement对象。
Statement stmt = conn.createStatement();
//执行sql语句(增删改的)
int result = stmt.executeUpdate(sql);
System.out.println("恭喜你执行成功"+result+"条记录!");

}

/**
* 查询所有用户的方法
* @param conn
* @param sql
* @return
* @throws SQLException
*/
public static List<User> queryDB(Connection conn, String sql) throws SQLException {
//获得执行sql语句的对象。Statement对象或者PreparedStatement对象。
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<User> userList = new ArrayList<User>();
//判断表中有无记录
while(rs.next()) {
User user = new User();
String userId = rs.getString(1);
String userName = rs.getString(2);
String userPassword = rs.getString(3);
user.setUser_id(userId);
user.setUser_name(userName);
user.setUser_password(userPassword);
userList.add(user);
}
return userList;
}



public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = ConnectionUtil.getConn();
//updateDB(conn, "insert into user_list values('4','zhaoliu','aa')");
//updateDB(conn, "update user_list set user_password='123456' where user_name='zhangsan'");
//updateDB(conn, "delete from user_list where user_id='1'");
List<User> userList = queryDB(conn, "select * from user_list");
for(int i = 0;i < userList.size();i ++) {
System.out.print(userList.get(i).getUser_id()+"--"+userList.get(i).getUser_name()+"--"+userList.get(i).getUser_password());
System.out.println("");
}
}
}



Oracle表结构:



下篇分享整合版的,每天只需要进步一点点。。。

展开阅读全文

JDBC-ODBC Bridge连接mysql的问题~

03-09

我是用JDBC-ODBC Bridge连接的数据库,数据库用的是mysql4.1,odbc驱动用的是mysql odbc 3.51 driver,已经建立了一个库yyy,库中有一个表qinshi,标的结构为qinshi(name char(20),age int,dorm char(20));并且已经有数据,也在odbc中注册了,但是在连接的时候总会出现问题,相关代码如下!rnrnimport java.sql.*;rnpublic class testrnrn static rn try rn Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");rn System.out.println("success loading JDBC-ODBC Bridge Driver...");rn catch(Exception e)rn System.out.println("Error loading JDBC-ODBC Bridge Driver...");rn e.printStackTrace();rn rn rn public static void main(String args[])rn tryrn Connection con1=DriverManager.getConnection("jdbc:odbc:yyy","root","");//**数据库没有密码,odbc设置处也没有设置密码**rn System.out.println("Success eastablishing the Connection...");rn Statement stm1=con1.createStatement();rn ResultSet rt=stm1.executeQuery("select * from qinshi where dorm='7412';");rn System.out.println("Success selest database...");rn System.out.println("Result of selest database is...");rn while(rt.next())rn rn String a=rt.getString(2);rn int b=rt.getInt(3);rn String c=rt.getString(4);rn System.out.println(a+" "+b+" "+c);rn rn con1.close();rn catch(Exception e)rn e.printStackTrace();rn rn rnrn在建立连接的时候,总会出现这样的异常rnrnC:\javawork>java testrnsuccess loading JDBC-ODBC Bridge Driver...rnjava.sql.SQLException: [MySQL][ODBC 3.51 Driver]Access denied for user 'root'@'lrnocalhost' (using password: NO)rn at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)rn at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)rn at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)rn at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3rn23)rn at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)rn at java.sql.DriverManager.getConnection(DriverManager.java:512)rn at java.sql.DriverManager.getConnection(DriverManager.java:171)rn at test.main(test.java:15)rnrn如果用access的时候,就可以没有什么问题,我想了挺长时间也没有解决:既然程序能够正常编译,并能够在access下正常运行,说明程序本身并没有什么问题;如果在mysql的命令行下执行相关的查询时,也能正常查询,说明数据库也是好的;odbc设置也不应该有什么错误的。rnrn如果在odbc设置处给用户名root加上密码(例如111),并在程序做标记的地方作相应的改动的话Connection con1=DriverManager.getConnection("jdbc:odbc:yyy","root","111");则会出现这样的提示:rnrnC:\javawork>java testrnsuccess loading JDBC-ODBC Bridge Driver...rnjava.sql.SQLException: [MySQL][ODBC 3.51 Driver]Client does not support authentirncation protocol requested by server; consider upgrading MySQL clientrn at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)rn at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)rn at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)rn at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3rn23)rn at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)rn at java.sql.DriverManager.getConnection(DriverManager.java:512)rn at java.sql.DriverManager.getConnection(DriverManager.java:171)rn at test.main(test.java:15)rnrn我现在真是很无奈,请各位大虾指教! 论坛

没有更多推荐了,返回首页