Current location - Training Enrollment Network - Books and materials - The book management system uses MySQL database. How to connect the database with Java code?
The book management system uses MySQL database. How to connect the database with Java code?
driver

string driver = " com . MySQL . JDBC . driver ";

// URL points to the database name mydb to be accessed.

string URL = " JDBC:MySQL:// 127 . 0 . 0 . 1:3306/mydb ";

//User name

String user = " root

MySQL password for Java connection.

String password = " root

Try {

//Load the driver

Class.forName (driver);

//Continuous database

connection conn = driver manager . getconnection(URL,user,password);

If (! conn.isClosed())

System.out.println ("Successfully connected to the database!" );

//statements are used to execute SQL statements.

Statement statement = conn.createstatement ();

//SQL statement to be executed

string SQL = " select * from student ";

ResultSet rs = statement. execute query(SQL);

while(rs.hasNext()){

//Traverse to get the result

}