Get the App
SLTechnology News&Howtos  ›  Database  › 

How to access mysql database directly by JDBC

Shulou Source: shulou.com Published: 2022-05-31 17:56:28 09月18日 Update

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

Tags: Data database learning more help practical next articles methods theories knowledge articles websites materials follow questions easy to use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi Shulou Tech Info OPPO Reno NVidia