java连接mysql
网址: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.编写代码
- 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(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。