首先下载 mysql-connector-java-5.1.10-bin.jar
package comm;
import java.sql.*;
public class ConnectMysql
{
public static void main(String args[]) throws Exception
{
Connection con = null;
Statement sm = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://127.0.0.1/test";
con = DriverManager.getConnection(url, "root", "123456");
sm = con.createStatement();
rs = sm.executeQuery("select * from fan");
while (rs.next())
{
System.out.println(rs.getString("name"));
}
if (rs != null)
rs.close();
if (sm != null)
sm.close();
if (con != null)
con.close();
}
}
|
阅读(922) | 评论(0) | 转发(0) |