How to access mysql database directly by JDBC
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