An example of how java is connected to mysql
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!