Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access mysql database directly by JDBC

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how JDBC directly accesses mysql database". In daily operation, I believe many people have doubts about how JDBC directly accesses mysql database. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how JDBC directly accesses mysql database". Next, please follow the editor to study!

Import com.mysql.jdbc.Driver

Import java.sql.Connection

Import java.sql.Statement

Import java.sql.DriverManager

Import java.sql.ResultSet

Import java.sql.ResultSetMetaData

Import java.io.*

Import java.sql.*

Public class Database {

PrivateConnection connection = null

Public boolean Init (String url, String userName, String password) throws Exception

{

Class.forName (com.mysql.jdbc.Driver) .newInstance ()

Connection = DriverManager.getConnection (url, userName, password)

Return true

}

Public void Test () throws Exception

{

Statement st = connection.createStatement ()

/ / Insert

/ / st.execute ("Insert into setup values ('13th,' 22')")

/ / Test Blob field

/ / Write Test

File file1 = new File ("Database.java")

Int len1 = (int) file1.length ()

InputStream fis1 = new java.io.FileInputStream (file1)

PreparedStatement ps = connection.prepareStatement ("INSERT INTO setup VALUES (?)")

Ps.setString (1,250)

Ps.setString (2, "250ddd")

Ps.setAsciiStream (3, fis1, len1)

Ps.execute ()

ResultSet r = st.executeQuery ("select * from setup")

If (ringing = null)

{

ResultSetMetaData m = r.getMetaData ()

While (r.next ())

{

For (int I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report