Home Database Mysql Tutorial JDBC 入门(二)_MySQL

JDBC 入门(二)_MySQL

Jun 01, 2016 pm 02:08 PM
jdbc url use getting Started Establish connect driver

JDBC

你需要做的第一事情是你与想要使用的 DBMS 建立一个连接。这包含 2 个步骤:装载驱动程序并建立连接。

装载驱动程序
  装载驱动程序只需要非常简单的一行代码。例如,你想要使用 JDBC-ODBC 桥驱动程序, 可以用下列代码装载它:

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  你的驱动程序文档将告诉你应该使用的类名。例如, 如果类名是 jdbc.DriverXYZ ,你将用代码以下的代码装载驱动程序:

    Class.forName("jdbc.DriverXYZ");

  你不需要创建一个驱动程序类的实例并且用 DriverManager 登记它,因为调用 Class.forName 将自动将加载驱动程序类。如果你曾自己创建实例,你将创建一个不必要的副本,但它不会带来什么坏处。

  加载 Driver 类后,它们即可用来与数据库建立连接。

建立连接
  第二步就是用适当的驱动程序类与 DBMS 建立一个连接。下列代码是一般的做法:

    Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");

  这个步骤也非常简单,最难的是怎么提供 url。如果你正在使用 JDBC-ODBC 桥, JDBC URL 将以 jdbc:odbc 开始:余下 URL 通常是你的数据源名字或数据库系统。因此,假设你正在使用 ODBC 存取一个叫 "Fred" 的 ODBC 数据源,你的 JDBC URL 是 jdbc:odbc:Fred 。把 "myLogin" 及 "myPassword" 替换为你登陆 DBMS 的用户名及口令。如果你登陆数据库系统的用户名为 "Fernanda" 口令为 "J8",只需下面的 2 行代码就可以建立一个连接:

String url = "jdbc:odbc:Fred";
Connection con = DriverManager.getConnection(url,"Fernanda", "J8");
  如果你使用的是第三方开发了的 JDBC驱动程序,文档将告诉你该使用什么 subprotocol, 就是在 JDBC URL 中放在 jdbc 后面的部分。例如, 如果驱动程序开发者注册了 acme 作为 subprotocol, JDBC URL 的第一和第二部分将是 jdbc:acme。驱动程序文档也会告诉你余下 JDBC URL 的格式。JDBC URL 最后一部分提供了定位数据库的信息。

  如果你装载的驱动程序识别了提供给 DriverManager.getConnection 的 JDBC URL ,那个驱动程序将根据 JDBC URL 建立一个到指定 DBMS 的连接。正如名称所示,DriverManager 类在幕后为你管理建立连接的所有细节。除非你是正在写驱动程序,你可能无需使用此类的其它任何方法,一般程序员需要在此类中直接使用的唯一方法是 DriverManager.getConnection。

  DriverManager.getConnection 方法返回一个打开的连接,你可以使用此连接创建 JDBC statements 并发送 SQL 语句到数据库。在前面的例子里,con 对象是一个打开的连接,并且我们要在以后的例子里使用它。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
UCM in Windows 11/10 - UCSI VPI device driver error UCM in Windows 11/10 - UCSI VPI device driver error Mar 24, 2024 am 09:50 AM

UCM-UCSIVPI is crucial to your computer because it is responsible for managing the USB Type-C port. When you encounter error messages for this driver in Device Manager, it will affect the normal working of your USB Type-C port. Therefore, it is crucial to resolve this issue promptly and this article will guide you on how to resolve the issues caused by this error message. UCM-UCSIACPI, Windows has stopped this device because it reported a problem (code 43). What is UCM—UCSIVPI device driver? Microsoft provides a driver that complies with the USB Type-C Connector System Software Interface (UCSI) specification for TPI transmission. If your system is configured

What should I do if Win11 fails to load the driver? What to do if the driver cannot be loaded on this device? What should I do if Win11 fails to load the driver? What to do if the driver cannot be loaded on this device? Apr 11, 2024 pm 05:01 PM

This article aims to solve the "The driver cannot be loaded on this device" error message that Win11 users encounter when trying to install or update drivers. By reading this article, Win11 users will be able to solve the "The driver cannot be loaded on this device" problem, thereby ensuring the stability of the system and the normal operation of the hardware device. Program Compatibility Assistant prompts: The driver cannot be loaded on this device. Method 1: 1. Press the [Win+X] key combination, or [right-click] click [Windows Start Logo] on the taskbar, and open the hidden menu item , select [Terminal Administrator]; 2. User Account Control window, do you want to allow this application to make changes to your device? Click【

A Diffusion Model Tutorial Worth Your Time, from Purdue University A Diffusion Model Tutorial Worth Your Time, from Purdue University Apr 07, 2024 am 09:01 AM

Diffusion can not only imitate better, but also "create". The diffusion model (DiffusionModel) is an image generation model. Compared with the well-known algorithms such as GAN and VAE in the field of AI, the diffusion model takes a different approach. Its main idea is a process of first adding noise to the image and then gradually denoising it. How to denoise and restore the original image is the core part of the algorithm. The final algorithm is able to generate an image from a random noisy image. In recent years, the phenomenal growth of generative AI has enabled many exciting applications in text-to-image generation, video generation, and more. The basic principle behind these generative tools is the concept of diffusion, a special sampling mechanism that overcomes the limitations of previous methods.

Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Aug 01, 2024 pm 03:28 PM

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to disable Windows Update from automatically updating drivers in win11? How to disable Windows Update from automatically updating drivers in win11? Jun 26, 2024 am 12:18 AM

When you connect any new hardware device to the system, Windows will automatically try to install the driver for it. When the system's built-in driver package cannot be recognized, it will automatically try to connect to Windows Update to search and install the driver. Windows can also automatically update device drivers through Windows Update without user interaction. Although this function seems convenient, under certain circumstances, the feature of automatically updating drivers can cause trouble for users. For example, users' video workflows such as DaVinciResolve, Adobe Premiere, etc. need to use a specific old version of Nvidia Studio driver. As a result, Windows

See all articles