关于JDBC
Posted degui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JDBC相关的知识,希望对你有一定的参考价值。
说道JDBC(Java Database Connectivity,Java 数据库连接),我们先说说ODBC(Open DataBase Connectivity,开放式数据库连接)是Microsoft提供的应用程序接口,用于访问数据库.它使用结构化查询语句作为其数据库的语言,并提供了插入,修改和删除数据以及从数据库中获取信息的各种功能。
一.使用JDBC-ODBC桥方式连接和操作数据库
1.配置ODBC数据源
应用程序->ODBC数据源->( Sql Server mysql Oracle Access)
2.配置
1>控制面板
2>管理工具
3>ODBC数据源
4>添加
5>选择sql server单击完成
到此为止,数据源testSqlserver配置完成。
3.加载JDBC-ODBC桥驱动
JDBC-ODBC桥接: sun.jdbc.JdbcOdbcDriver
oracle连接: oracle.jdbc.driver.OracleDriver
MySql连接: org.gjt.mm.mysql.Driver
//加载驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//获取一个Connection对象
Connection conn = DriverManager.getConnection("jdbc:odbc:testSqlserver");
//连接建立成功后,创建Statement接口实例,也可创建PreparedStatement
Statement stmt = conn.createStatement();
PreparedStatement stmt = conn.prepareStatement(string sql) 效率比Statement对象的效率高很多
//实用Statement对象执行SQL
ResultSet rs = stmt.executeQuery("select * from bookDB.dbo.books");
二.实用纯JDBC驱动的方式连接和操作数据库
sqlServer桥接: com.microsoft.sqlserver.jdbc.SQLServerDriver
oracle连接: oracle.jdbc.driver.OracleDriver
MySql连接: org.gjt.mm.mysql.Driver
示例JDBC连接sqlserver
1>建表结构
USE [bookDB] GO /****** Object: Table [dbo].[books] Script Date: 2018/5/27 12:19:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[books]( [book_id] [int] IDENTITY(1,1) NOT NULL, [book_name] [varchar](50) NULL, [book_author] [varchar](50) NULL, [book_publish] [varchar](50) NULL, [book_page] [int] NULL, [book_price] [float] NULL, CONSTRAINT [PK__books__490D1AE137E3FA29] PRIMARY KEY CLUSTERED ( [book_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
2>books的 增 删 改 查
package JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BookInfo { /** * @param args */ //加载驱动 static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //url 地址 static String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=bookDB"; static PreparedStatement pstmt= null; static Statement stmt = null; static Connection conn = null; static String userName = "sa"; static String password = "admin"; static int rownum = 0; static ResultSet rs= null; public static void main(String[] args) { //加载驱动 try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } //得到一个连接 try { conn = DriverManager.getConnection(url,userName,password); } catch (SQLException e) { e.printStackTrace(); } //增加 //Insert(); //删除 //Delete(); //修改 Update(); //查询 Select(); } //插入 public static void Insert(){ //创建PreparedStatement语句 try { pstmt = conn.prepareStatement("insert into books values(?,?,?,?,?)"); pstmt.setInt(1,4); pstmt.setString(2,"book_author"); pstmt.setString(3,"book_author"); pstmt.setInt(4,8); pstmt.setFloat(5,0); rownum = pstmt.executeUpdate(); if(rownum>0){ System.out.println("插入成功"); } } catch (Exception e) { e.printStackTrace(); } } //删除 public static void Delete(){ //创建PreparedStatement语句 try { pstmt = conn.prepareStatement("delete from books where book_id = ?"); pstmt.setInt(1,11); rownum = pstmt.executeUpdate(); if(rownum>0){ System.out.println("删除成功"); } } catch (Exception e) { e.printStackTrace(); } } //修改 public static void Update(){ //创建PreparedStatement语句 try { pstmt = conn.prepareStatement("update books set book_name = ? where book_id = ?"); pstmt.setString(1,"已修改"); pstmt.setInt(2,10); rownum = pstmt.executeUpdate(); if(rownum>0){ System.out.println("修改成功"); } } catch (Exception e) { e.printStackTrace(); } } //查询 public static void Select(){ //创建createStatement对象 try { stmt = conn.createStatement(); rs = stmt.executeQuery("select * from books"); while(rs.next()){ System.out.println("编号"+ rs.getInt("book_id")+","+ "书名"+ rs.getString("book_name")+""); System.out.println(); } } catch (SQLException e) { e.printStackTrace(); } } }
以上是关于关于JDBC的主要内容,如果未能解决你的问题,请参考以下文章
mysql jdbc源码分析片段 和 Tomcat's JDBC Pool