电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1891 人浏览分享

JDBC连接MySQL5.7的方法

[复制链接]
1891 0

这篇文章主要介绍了JDBC连接MySQL5.7的方法,需要的朋友可以参考下


1.首先准备mysql 和eclipse环境,在环境搭建好之后,从eclipse官网下载jdbc的驱动包,
下载地址http://dev.mysql.com/downloads/connector/j/

2.从下载的文件中取出mysql-connector-java-5.1.31-bin.jar,放到工程中,并导入路径
方法:右击工程名->Build Path->Configure Build Path,选择Add External JAR...

找到mysql-connector-java-5.1.31-bin.jar所在的位置,然后将驱动包加载到项目中,

20180506211311.png

3.写个例子测试一下

  1. package testmysql;
  2. import java.sql.*;
  3. public class Test {

  4.   public static void main(String[] args) {
  5.     String driver = "com.mysql.jdbc.Driver";
  6.     String URL = "jdbc:mysql://localhost:3306/student";
  7.     Connection con = null;
  8.     try
  9.     {
  10.       Class.forName(driver);
  11.     }
  12.     catch(java.lang.ClassNotFoundException e)
  13.     {
  14.       System.out.println("Connect Successfull.");
  15.       System.out.println("Cant't load Driver");
  16.     }
  17.     try   
  18.     {                                         
  19.       con=DriverManager.getConnection(URL,"root","root");
  20.       System.out.println("Connect Successfull.");
  21.     }  
  22.     catch(Exception e)
  23.     {
  24.       System.out.println("Connect fail:" + e.getMessage());
  25.     }
  26.   }
  27. }
复制代码


连接上数据库之后,可以根据表中的内容进行数据库表的查询,首先表中要有内容,
将一些信息输入到表中之后即可使用SQL语言进行查询

  1. import java.sql.*;  
  2. public class Main {  
  3.   
  4.   public static void main(String[] args) {  
  5.     String driver = "com.mysql.jdbc.Driver";  
  6.     String URL = "jdbc:mysql://localhost:3306/xiaolu";  
  7.     Connection con = null;
  8.     ResultSet rs = null;
  9.     Statement st = null;
  10.     String sql = "select * from student";
  11.     try  
  12.     {  
  13.       Class.forName(driver);  
  14.     }  
  15.     catch(java.lang.ClassNotFoundException e)  
  16.     {  
  17.       // System.out.println("Connect Successfull.");  
  18.       System.out.println("Cant't load Driver");  
  19.     }  
  20.     try   
  21.     {                                          
  22.       con=DriverManager.getConnection(URL,"root","root");  
  23.       st=con.createStatement();
  24.       rs=st.executeQuery(sql);
  25.       if(rs!=null) {
  26.         ResultSetMetaData rsmd = rs.getMetaData();
  27.         int countcols = rsmd.getColumnCount();
  28.         for(int i=1;i<=countcols;i++) {
  29.           if(i>1) System.out.print(";");
  30.           System.out.print(rsmd.getColumnName(i)+" ");
  31.         }
  32.         System.out.println("");
  33.         while(rs.next()) {
  34.           System.out.print(rs.getString("sno")+" ");
  35.           System.out.print(rs.getString("sname")+" ");
  36.           System.out.print(rs.getString("ssex")+" ");
  37.           System.out.print(rs.getString("sage")+" ");
  38.           System.out.println(rs.getString("sdept")+" ");
  39.         }
  40.       }
  41.       //System.out.println("Connect Successfull.");  
  42.       System.out.println("ok");
  43.       rs.close();
  44.       st.close();
  45.       con.close();
  46.     }   
  47.     catch(Exception e)  
  48.     {  
  49.       System.out.println("Connect fail:" + e.getMessage());  
  50.     }  
  51.   }  
  52. }
复制代码



关于JDBC连接MySQL5.7的文章就介绍到这。



您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.