#1、JDBC基础 #JDBC简介:JDBC(Java Database Connectivity)是一种可以执行SQL的Java API,通过它可以用一种API操作不同的数据库. #JDBC驱动:不同数据库间,标准的SQL语句可以移植,而数据库实际通信协议及某些数据库特征不可移植,因此,JDBC和数据库之间须还有一层,用于将JDBC调用映射成特定的数据库调用,此特殊层就是JDBC驱动程序. 一、连接各种数据库方式速查表 下面罗列了各种数据库使用JDBC连接的方式,可以作为一个手册使用。 1、Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password); #传统JDBC访问数据库步骤 》通过Class.forName(String driverClass)注册数据库驱动 》通过DriverManager.getConnection(String url,String user,String password)获得数据库连接对象 》通过Connnection.createStatement()或者Connection.createPreparedStatement(String sql)创建相应的Statement对象 》通过Statement.execute(String sql)或者PreparedStatement.execute()执行相应的SQL,并返回ResultSet对象 》操作ResultSet、PreparedStatementCallback访问数据库 作用类似于StatementCallback接口,只是使用了PreparedStatement对象 /* *示例 */ public class ExecutePreparedStatementCallback { public static void main(String[] args)throws Exception { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass("com.mysql.jdbc.Driver"); ds.setJdbcUrl("jdbc:mysql://localhost:3306/j2ee"); ds.setUser("root"); ds.setPassword("32147"); ds.setMaxPoolSize(40); ds.setMinPoolSize(2); ds.setMaxStatements(180); //创建一个JdbcTemplate JdbcTemplate jt = new JdbcTemplate(); //为JdbcTemplate指定DataSource jt.setDataSource(ds); jt.execute("update mytable set name=? where name = 'xx'", new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement pstmt)throws SQLException { pstmt.setString(1,"-----"); pstmt.execute(); return null; } } ); } }Sql Server7.0/2000数据库 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB为数据库名 Connection conn= DriverManager.getConnection(url);access数据库直连用ODBC的 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); Connection conn = DriverManager.getConnection(url,"",""); Statement stmtNew=conn.createStatement() ;DB2数据库 Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);DriverManagerDataSource类继承Spring的抽象类AbstractDataSource,实际上,DriverManagerDataSource常被作为SmartDataSource的一个实现,而不是作为JDBC2.0规范的DataSource实现.因为DriverManagerDataSource并不具备连接池的能力.JDBC驱动:不同数据库间,标准的SQL语句可以移植,而数据库实际通信协议及某些数据库特征不可移植,因此,JDBC和数据库之间须还有一层,用于将JDBC调用映射成特定的数据库调用,此特殊层就是JDBC驱动程序.
JAVA关于JDBC连接数据库方法大全
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《JAVA关于JDBC连接数据库方法大全》
文章链接:https://www.skykkk.com/archives3292.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
文章名称:《JAVA关于JDBC连接数据库方法大全》
文章链接:https://www.skykkk.com/archives3292.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
相关推荐
- 暂无文章