After the project is imported into Mysql-connector-java-5.1.7-bin.jar;
public static void Main (string[] args)
{
Driver name
String Driver = "Com.mysql.jdbc.Driver";
The URL points to the database name you want to access Scutcs
String url = "Jdbc:mysql://127.0.0.1:3305/test";
User name when MySQL is configured
String user = "root";
Password for MySQL configuration
String password = "123456";
Try
{
Load Driver
Class.forName (driver);
Continuous database
Connection conn = drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ())
{
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
}
statement used to execute SQL statements
Statement Statement = Conn.createstatement ();
The SQL statement to execute
String sql = "SELECT * from Test1";
String name = NULL;
Result set
ResultSet rs = statement.executequery (SQL);
while (Rs.next ())
{
Select sname This column of data
Name = rs.getstring ("title");
Output results
System.out.println (rs.getstring ("id") + "\ T" + name);
}
Rs.close ();
Conn.close ();
}
catch (ClassNotFoundException e)
{
System.out.println ("Sorry,can ' t find the driver!");
E.printstacktrace ();
}
catch (SQLException e)
{
E.printstacktrace ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
Java Connection MySQL Database