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所在的位置,然后将驱动包加载到项目中,



3.写个例子测试一下

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

	public static void main(String[] args) {
		String driver = "com.mysql.jdbc.Driver";
		String URL = "jdbc:mysql://localhost:3306/student";
		Connection con = null;
		try
		{
			Class.forName(driver);
		}
		catch(java.lang.ClassNotFoundException e)
		{
			System.out.println("Connect Successfull.");
			System.out.println("Cant't load Driver");
		}
		try   
		{                                                                               
			con=DriverManager.getConnection(URL,"root","root");
			System.out.println("Connect Successfull.");
		} 
		catch(Exception e)
		{
			System.out.println("Connect fail:" + e.getMessage());
		}
	}
}

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

import java.sql.*;  
public class Main {  
  
    public static void main(String[] args) {  
        String driver = "com.mysql.jdbc.Driver";  
        String URL = "jdbc:mysql://localhost:3306/xiaolu";  
        Connection con = null;
        ResultSet rs = null;
        Statement st = null;
        String sql = "select * from student";
        try  
        {  
            Class.forName(driver);  
        }  
        catch(java.lang.ClassNotFoundException e)  
        {  
           // System.out.println("Connect Successfull.");  
            System.out.println("Cant't load Driver");  
        }  
        try     
        {                                                                                 
            con=DriverManager.getConnection(URL,"root","root");  
            st=con.createStatement();
            rs=st.executeQuery(sql);
            if(rs!=null) {
            	ResultSetMetaData rsmd = rs.getMetaData();
            	int countcols = rsmd.getColumnCount();
            	for(int i=1;i<=countcols;i++) {
            		if(i>1) System.out.print(";");
            		System.out.print(rsmd.getColumnName(i)+" ");
            	}
            	System.out.println("");
            	while(rs.next()) {
            		System.out.print(rs.getString("sno")+"  ");
            		System.out.print(rs.getString("sname")+"  ");
            		System.out.print(rs.getString("ssex")+"  ");
            		System.out.print(rs.getString("sage")+"  ");
            		System.out.println(rs.getString("sdept")+"  ");
            	}
            }
            //System.out.println("Connect Successfull.");  
            System.out.println("ok");
            rs.close();
            st.close();
            con.close();
        }   
        catch(Exception e)  
        {  
            System.out.println("Connect fail:" + e.getMessage());  
        }  
    }  
}



  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值