elipse与数据库Mysql连接,并实现创建数据表的功能

elipse与数据库Mysql连接,并实现创建数据表的功能

一、 elipse与数据库相连

1.1在eclipse中加载jdbc驱动程序:

点击windows–>Preferences–>Java–>Bulid Path–>User Libraries
在这里插入图片描述
点击右侧NEW按钮,输入创建的libraries的名称(可命名为:jdbc),并打上下面的对勾。
在这里插入图片描述
回到上一级界面,点击Add External JARs,打开到你的jdbc存放的目录,打开,点击Apply and Close.
在这里插入图片描述

1.2项目中导入jar包。

选择想要添加jar包的项目,右键–>Build Path-Configure Build Path
在这里插入图片描述
点击右侧Add Library… -User Library-Next。打上对勾点击finish
在这里插入图片描述
选择刚刚创建的libraries
在这里插入图片描述
点击finish。回到上一级便可以看到刚刚导入的jar包。
这样便可以在你的项目中看到导入的jdbc了。
在这里插入图片描述

二、测试连接数据库

import java.sql.Connection; 
import java.sql.DriverManager; 
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; 
	//其中com.mysql.jdbc.Driver为刚刚导入的jar包中一个类。
	// 定义MySQL数据库的连接地址 
	public static final String DBURL = 
	"jdbc:mysql://localhost:3306/mysql0506?&useSSL=false" ; 
	//jdbc:指明连接驱动程序
	//mysql:为要连接的数据库,localhost为本地连接服务器,3306是连接端口,mysql0605为要连接的数据库。
	//useSSL=false,uerSSL为MySQL在高版本需要指明是否进行SSL连接。
	//不指明该连接的话,elipse中可能会报错。
	
   // MySQL数据库的连接用户名 
	public static final String DBUSER = "root" ; 
	// MySQL数据库的连接密码 
	public static final String DBPASS = "123456" ; 
	public Connection getCon()throws Exception{
	Class.forName(DBDRIVER);
	Connection con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
	return con;
}
   public void closeCon(Connection con)throws Exception{
   if(con!=null)
   	{
    con.close();
  	}
  }

测试代码:

test_jdbc t=new test_jdbc();
		Connection conn = null ; 
		try {
		conn=t.getCon();
		System.out.println("连接成功");
		}
		catch(Exception e) {
			System.out.println("连接失败");
			System.out.println(e);
		}

程序运行截图如下:
在这里插入图片描述

SSL协议提供服务主要:

   1)认证用户服务器,确保数据发送到正确的服务器;    .
   2)加密数据,防止数据传输途中被窃取使用;
   3)维护数据完整性,验证数据在传输过程中是否丢失;

当前支持SSL协议两层:
SSL记录协议(SSL Record Protocol):建立靠传输协议(TCP)高层协议提供数据封装、压缩、加密等基本功能支持
SSL握手协议(SSL Handshake Protocol):建立SSL记录协议用于实际数据传输始前通讯双进行身份认证、协商加密
算法、 交换加密密钥等。

三、利用eclipse实现在mysql中创建数据表的功能:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Scanner;
import java.util.Set; 
public class test_jdbc {
	// 定义MySQL的数据库驱动程序 
	public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; 
	// 定义MySQL数据库的连接地址 
	public static final String DBURL = 
	"jdbc:mysql://localhost:3306/mysql0506?&useSSL=false" ; 
	// MySQL数据库的连接用户名 
	public static final String DBUSER = "root" ; 
	// MySQL数据库的连接密码 
	public static final String DBPASS = "123456" ; 
	public Connection getCon()throws Exception{
	Class.forName(DBDRIVER);
	Connection con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
	return con;
}
   public void closeCon(Connection con)throws Exception{
   if(con!=null)
   	{
    con.close();
  	}
  }
	public static void main(String[] args) throws Exception { 
		test_jdbc t=new test_jdbc();
		Connection conn = null ; 
		// 数据库连接 
		Statement stmt = null ;
		// 数据库操作 
		ResultSet rs = null ; 
		// 保存查询结果 
		String tableName; 
		HashMap<String,String> field = new HashMap<String,String>(); 
		String fieldType; 
		String fieldName; 
		StringBuffer sqlBuf = new StringBuffer(); 
		System.out.println("请输入数据表名称:"); 
		Scanner scan = new Scanner(System.in); 
		tableName = scan.next(); 
		while(true) 
		{ 
		System.out.println("请输入字段名称:"); 
		fieldName = scan.next(); 
		System.out.println("请输入字段类型:"); 
		fieldType = scan.next(); 
		if(fieldName.equals("end")) { 
		break; 
		} 
		if(fieldType.equals("end")) { 
		break; 
		} 
		field.put(fieldName, fieldType); 
		} 
		Set<String> key = field.keySet(); 
		Iterator<String> iter = key.iterator(); 
		int length=field.size();
		while(length>1) 
		{ 
			length--;
		String str=iter.next();
		sqlBuf.append(str+" "); 
		sqlBuf.append(field.get(str)+","); 
		} 
		String str=iter.next();
		sqlBuf.append(str+" "); 
		sqlBuf.append(field.get(str)); 
		StringBuffer temp=new StringBuffer(); 
		temp.append("CREATE TABLE "); 
		temp.append(tableName+"("); 
		temp.append(sqlBuf+")"); 
		String sql =new String(temp) ; 	 
		System.out.println(sql);
		try {
		conn=t.getCon();
		stmt = conn.createStatement() ; 
		// 实例化Statement对 
		stmt.execute(sql); 
		System.out.println("创建表格成功");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		//rs.close() ; 
		// 关闭结果集 
		stmt.close() ; 
		// 操作关闭 
		t.closeCon(conn);
		// 数据库关闭 
		
	}
}

在这里插入图片描述
此时在mysql_front中便可以看到刚刚创建的数据表。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值