怎样使用Java连接Mysql数据

怎样使用Java连接Mysql数据库

    

    废话不多说上代码

    

package conM;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Cnn {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.sql.Connection con = null;                        
		String driver ="com.mysql.jdbc.Driver";            
		String url="jdbc:mysql://localhost:3306/ed";       
		String user="root";
		String password="1234";
		
		try {
			Class.forName(driver);
			con=DriverManager.getConnection(url, user, password);
			if(!con.isClosed())
				System.out.println("Successded connectingtothe Database");
			Statement statement = con.createStatement();
			String sql = "select * from emp";
			ResultSet rs=statement.executeQuery(sql);
			while(rs.next()) {
				String rowT="" ;
				for(int i=1;i<=rs.getMetaData().getColumnCount();i++) {              
					rowT+=rs.getString(rs.getMetaData().getColumnName(i))+" ";
				}
				System.out.println(rowT);
			}
			rs.close();                                               
			con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Sorry,can`t find the Driver!");  
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			  System.out.println("数据库数据成功获取!!");
		}
		
		
		
		
		
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值