1.下载jar包
网址:http://dev.mysql.com/downloads/
-->点击:"MySQL
Connector/J 5.1 — Generally Available (GA) release (recommended)"
-->点击:(mysql-connector-java-5.1.10.zip)后面的download按钮
-->点击:"? No
thanks, just take me to the downloads!"
-->随便选一个"HTTP"超链接,开始下载
2.在项目中导入下载的jar包 mysql-connector-java-5.1.10-bin.jar
3.打开mysql
4.编写代码
Java代码
- package Test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class TestMySql
- {
- public static void main(String[] args)
- {
- try
- {
- Statement stmt=null;
- ResultSet rs=null;
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称","root","123456");
- stmt = (Statement)conn.createStatement();
- rs = stmt.executeQuery("SELECT * from PetInfo");
- while (rs.next())
- {
- System.out.println(rs.getString(2));
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
package Test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class TestMySql
{
public static void main(String[] args)
{
try
{
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称","root","123456");
stmt = (Statement)conn.createStatement();
rs = stmt.executeQuery("SELECT * from PetInfo");while (rs.next())
{
System.out.println(rs.getString(2));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}