(The follow option is after you had installed the
mysql and configure the related environment)
Project->properties->Java Build Path->Libraries->Choose
Add External,
Choose the mysql-connector-java-5.1.13-bin.jar
package my;
import java.sql.*;
public class MysqlJdbc{
public
static void main(String args[]) {
String sqlDriver = "com.mysql.jdbc.Driver";
Connection connect = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载MYSQL JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
String url =
"jdbc:mysql://localhost:3306/hope?user=root&password=xiu1314";
Class.forName(sqlDriver).newInstance();
connect =
DriverManager.getConnection(url,"root","password");
//password is that you set when u install
the mysq
stmt = connect.createStatement();
rs = stmt.executeQuery("select * from comment");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
<!-- 正文结束 -->
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23578877/viewspace-1114539/
,如需转载,请注明出处,否则将追究法律责任。