The first step: download a JDBC driver package, for example, I used: Mysql-connector-java-5.1.17-bin.jar
The second step: the introduction of the download of the JDBC driver package, I used the MyEclipse, select their own to guide the package of items, right click to select Propertise, and then select Javabuild Path, right will appear libreries, point in, and then point add External JARs and then find the driver package you want to import. After the order Andexport, then select the package you imported.
Step three: load driver: Class.forName ("Com.mysql.jdbc.Driver");
Fourth Step: Connection Database: Connection conn=drivermanager.getconnection ("jdbc:mysql://localhost/database name", "Root", "123456");
Step Fifth: declare a Statement used to execute the SQL statement: Statement stmt=conn.createstatement ();
Step Sixth: Declare a result set to catch the data that executes the SQL statement: ResultSet rs=stmt.executequery ("SELECT * from table name");
The complete code is given below:
Copy Code code as follows:
try {
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("test Pass");
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost/myschool", "root", "123456");
SYSTEM.OUT.PRINTLN ("conn-------------" +conn);
Statement stmt=conn.createstatement ();
ResultSet rs=stmt.executequery ("SELECT * from admin");
while (Rs.next ()) {
String name=rs.getstring ("name");
String pwd=rs.getstring ("PWDs");
SYSTEM.OUT.PRINTLN ("name------" +name+ "--------pwd-" +pwd);
}
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}