Get the App
SLTechnology News&Howtos  ›  Database  › 

An example of how java is connected to mysql

Shulou Source: shulou.com Published: 2022-05-31 21:33:33 10月04日 Update

This article mainly explains "an example of how java is connected to mysql". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to connect java to mysql".

On the premise that mysql jdbc is installed correctly:

Import java.sql.*

Import java.lang.*

Public class ch2

{

Public static void main (String args [])

{

String driver = "org.gjt.mm.mysql.Driver"

String url = "jdbc:mysql://127.0.0.1:3306/jsptest"

String user = "hukn"

String password= "hukn"

Try

{

Class.forName (driver)

}

Catch (Exception E)

{

System.out.print ("unable to load driver:" + driver)

}

Try

{

Connection con=DriverManager.getConnection (url,user,password)

If (! con.isClosed ()

System.out.println ("database opened successfully")

Statement smt=con.createStatement ()

Smt.executeUpdate ("insert into test (name,adress) values ('China', 'Chengdu')

Smt.close ()

Con.close ()

}

Catch (SQLException SE)

{

System.out.print (failed to open database)

}

}

} at this point, I believe you have a deeper understanding of "how java is connected to mysql". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Content data database learning practical deeper successful interest premise practical practical easy to operate method more friends website channel China Chengdu query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux macOS NVidia Shulou Technology