java:连接MySQ数据库
- 下载Jar包:mysql-connector-java-5.1.38-bin.jar
- 注册驱动
- 创建数据库连接
- 获取表达式
- 执行sql语句并获取结果集
- 打印结果集
- 示例代码:
- 
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Mysql { public Connection conn; { try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //创建数据库链接 conn = DriverManager.getConnection("jdbc:mysql://localhost/spring", "root", "root"); //获取表达式 Statement stmt = conn.createStatement(); //执行Sql语句获取结果集 String sql = "select * from user"; ResultSet res = stmt.executeQuery(sql); //打印结果集 while(res.next()) { System.out.print("the id: "); System.out.println(res.getInt(1)); System.out.print("the name: "); System.out.println(res.getString("name")); System.out.print("the email: "); System.out.println(res.getString("email")); System.out.println(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { new Mysql(); } } the id: 1 
 the name: zhangsan
 the email: zhangsan@163.comthe id: 2 
 the name: lisi
 the email: lisi@qq.com
    http://www.cnblogs.com/makexu/

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号