derby 转mysql_请教一下,把下面连接derby数据库转换成连接mysql应该怎样修改(急)!!...

代码如下:packagecom.mwq.dao;importjava.io.File;importjava.sql.*;importjavax.swing.JOptionPane;publicclassJDBC{privatestaticfinalStringDRIVERCLASS="org.apache.derby.jdbc....

代码如下:

package com.mwq.dao;

import java.io.File;

import java.sql.*;

import javax.swing.JOptionPane;

public class JDBC {

private static final String DRIVERCLASS = "org.apache.derby.jdbc.EmbeddedDriver"; // 数据库驱动

private static final String URL = "jdbc:derby:db_ExpressLetter";// 数据库URL

private static final ThreadLocal threadLocal = new ThreadLocal(); // 创建用来保存数据库连接的线程

private static Connection conn = null;// 数据库连接

static { // 通过静态方法加载数据库驱动,并且在数据库不存在的情况下创建数据库

try {

Class.forName(DRIVERCLASS).newInstance(); // 加载数据库驱动

File dbExpressLetterFile = new File("db_ExpressLetter");// 创建数据库文件对象

if (!dbExpressLetterFile.exists()) {// 判断数据库文件是否存在

String[] sqls = new String[6];// 定义创建数据库的SQL语句

sqls[0] = "create table tb_info (num int not null,type_id int not null,content varchar(500) not null,primary key (num))";

sqls[1] = "create table tb_personnel (num int not null,type_id int not null,name varchar(8) not null,sex char(2) not null,birthday date not null,company varchar(50) not null,dept varchar(40) not null,duty varchar(30) not null,handset varchar(15) not null,email varchar(30) not null,primary key (num))";

sqls[2] = "create table tb_type (id int not null,name varchar(20) not null,used char(4) not null,primary key (id))";

sqls[3] = "create table tb_user (id int not null,name varchar(8) not null,sex char(2) not null,birthday date not null,id_card varchar(20) not null,password varchar(20) not null,freeze char(4) not null,primary key (id))";

sqls[4] = "create view v_info_type as SELECT tb_info.num, tb_type.name AS type_name, tb_info.content FROM tb_info INNER JOIN tb_type ON tb_info.type_id = tb_type.id ";

sqls[5] = "create view v_personnel_type as SELECT tb_personnel.num, tb_type.name AS type_name, tb_personnel.name, tb_personnel.sex, tb_personnel.birthday, tb_personnel.company, tb_personnel.dept, tb_personnel.duty, tb_personnel.handset, tb_personnel.email FROM tb_personnel INNER JOIN tb_type ON tb_personnel.type_id = tb_type.id";

conn = DriverManager.getConnection(URL + ";create=true");// 创建数据库连接

threadLocal.set(conn);// 保存数据库连接

Statement stmt = conn.createStatement();// 创建数据库连接状态对象

for (int i = 0; i < sqls.length; i++) {// 通过执行SQL语句创建数据库

stmt.execute(sqls[i]);// 执行SQL语句

}

stmt.close();// 关闭数据库连接状态对象

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static Connection getConnection() { // 创建数据库连接的方法

conn = (Connection) threadLocal.get(); // 从线程中获得数据库连接

if (conn == null) { // 没有可用的数据库连接

try {

conn = DriverManager.getConnection(URL);// 创建新的数据库连接

threadLocal.set(conn); // 将数据库连接保存到线程中

} catch (Exception e) {

String[] infos = { "未能成功连接数据库!", "请确认本软件是否已经运行!" };

JOptionPane.showMessageDialog(null, infos);// 弹出连接数据库失败的提示

System.exit(0);// 关闭系统

e.printStackTrace();

}

}

return conn;

}

展开

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值