mysql数据连接,由于mysql数据库支持数据类型的较多,会有些麻烦,但是总体而言,还是方便的。
相似地,在使用weka包连接mysql前,我们先回顾如何java直接连接mysql数据库;
1,eclipse连接mysql数据库(请先加载mysql-connector-java-5.1.18-bin.jar包)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Statement;
public class Jdbc2mysql {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://30.209.81.8:3306/rec_comment","root","......");
//连接URL为 jdbc:mysql//服务器地址/数据库名,后面的2个参数分别是登陆用户名和密码,请自行修改
System.out.println("Success connect Mysql server!");
Statement stmt = (Statement) connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from tb_rec_stat recstat0_ limit 10");
//user 为你表的名称
while (rs.next()) {
System.out.print(rs.getString("id")+"\t");
System.out.print(rs.getString("object_id")+"\t");
System.out.println(rs.getString("object_name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
成功后,如下图:
2.1,首先将原weka包复制一份到该项目下,然后用RAR打开这个项目下的weka包,准备修改文件;
2.2,按weka.jar - - weka文件夹 - - experiment 文件夹下,先删除 DatabaseUtils.props 文件
2.3,并在以上目录下找到 DatabaseUtils.props.mysql 文件,用notepad等编译工具打开,修改该文件;
2.4,做如下修改:
# JDBC driver (comma-separated list)
jdbcDriver=com.mysql.jdbc.Driver
# database URL
jdbcURL=jdbc:mysql://localhost:3306/db_predict(localhost为数据库地址,db_predict为数据库名)
# specific data types,去掉注释,改为weka支持的数据格式
string, getString() = 0; --> nominal
boolean, getBoolean() = 1; --> nominal
double, getDouble() = 2; --> numeric
byte, getByte() = 3; --> numeric
short, getByte()= 4; --> numeric
int, getInteger() = 5; --> numeric
long, getLong() = 6; --> numeric
float, getFloat() = 7; --> numeric
date, getDate() = 8; --> date
text, getString() = 9; --> string
time, getTime() = 10; --> date
BigDecimal,getBigDecimal()=11; -->nominal
# mysql-conversion
TINYINT=3
SMALLINT=4
SHORT=5
INTEGER=5
INT=5
INT_UNSIGNED=6
BIGINT=6
LONG=6
REAL=7
NUMERIC=2
DECIMAL=2
FLOAT=2
DOUBLE=2
CHAR=0
TEXT=0
VARCHAR=0
LONGVARCHAR=9
BINARY=0
VARBINARY=0
LONGVARBINARY=9
BIT=1
BLOB=8
DATE=8
TIME=8
DATETIME=8
TIMESTAMP=8
2.5,保存文件,修改文件名,去掉后缀名mysql,最后成为 DatabaseUtils.props 文件
2.6,在eclipse中调用weka包直连mysql数据库,编程,取数,顺带生成ARFF文件
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.experiment.InstanceQuery;
public class Weka2Mysql {
public static void main(String[] args) throws Exception{
// org.gjt.mm.mysql.Driver
InstanceQuery q=new InstanceQuery();
q.setUsername("root");
q.setPassword("0wlonewave!@#");
q.setQuery("select * from tb_rec_stat limit 10");
Instances data=q.retrieveInstances();
System.out.println(data);
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File("data\\testcsv1.arff"));
saver.writeBatch();
}
}
结果如下图:
项目中多了一个ARFF文件,如图: