Simple JDBC Small instance
Package Com.javajdbctest;import Java.sql.drivermanager;import Java.sql.resultset;import com.mysql.jdbc.Connection; Import Com.mysql.jdbc.statement;public class JDBCTestDemo1 {public static void main (String args[]) throws exception{// Load Driver class Class.forName ("Com.mysql.jdbc.Driver");//Get Database connection by DriverManager string url = "Jdbc:mysql://127.0.0.1/testdb"; String user = "root"; String password = "DDDDD"; Connection Connection = (Connection) drivermanager.getconnection (URL, user, password);//Get Connection object by statement object, Execute SQL statement Statement statement = (statement) connection.createstatement (); ResultSet ResultSet = Statement.executequery ("SELECT * from Customers");//Operation Resultsetwhile (Resultset.next ()) { System.out.println (resultset.getstring ("Cust_name"));} Close database connection resultset.close (); Statement.close (); Connection.close ();}}
This is the MySQL table
Connection succeeded, get table data:
Java JDBC Connection MySQL