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中便可以看到刚刚创建的数据表。