import java.sql.*;public class SelectSql {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
int num=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager
.getConnection("jdbc:mysql://localhost/arp2?user=root&password=root");
st = con.createStatement(); rs= st.executeQuery("select mac1, count(mac1) from broadcast group by mac1");
while (rs.next()) {
System.out.print(rs.getString(1)+" ");
if (rs.getInt(2) < 10) {
System.out.println(rs.getInt(2));
} else{
System.out.print(rs.getInt(2));
System.out.println(" insert into sql");
}
/*******************************************************
希望修改处 ^-^
//string str=rs.getString(1);
//new datatime();
//insert into stumermac values(id,time,str);
************************************************************
*/
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
System.out.println("sqlState:" + ex.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException se) {
se.printStackTrace();
} }
}
}
?????????????????????????????????????
提问:上边是个可运行的程序
运行结果是:
00:0c:f7:fb:02:18 17 insert into sql
00:1b:b9:a1:fb:9b 1
00:1b:b9:d7:ce:26 1
00:1b:b9:da:de:65 1
********************************************************************************
希望达到的目的:现在想把rs.getString(1)的值00:0c:f7:fb:02:18输入到同一个库的另外一个表中stumermac
他的结构是
mysql> desc stumermac;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| time | time | YES | | NULL | |
| mac | varchar(17) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+所以还要
1 添加的id
2 添加当前插入的时间time
最后当然还有mac------rs.getString(1)的值00:0c:f7:fb:02:18
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
int num=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager
.getConnection("jdbc:mysql://localhost/arp2?user=root&password=root");
st = con.createStatement(); rs= st.executeQuery("select mac1, count(mac1) from broadcast group by mac1");
while (rs.next()) {
System.out.print(rs.getString(1)+" ");
if (rs.getInt(2) < 10) {
System.out.println(rs.getInt(2));
} else{
System.out.print(rs.getInt(2));
System.out.println(" insert into sql");
}
/*******************************************************
希望修改处 ^-^
//string str=rs.getString(1);
//new datatime();
//insert into stumermac values(id,time,str);
************************************************************
*/
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
System.out.println("sqlState:" + ex.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException se) {
se.printStackTrace();
} }
}
}
?????????????????????????????????????
提问:上边是个可运行的程序
运行结果是:
00:0c:f7:fb:02:18 17 insert into sql
00:1b:b9:a1:fb:9b 1
00:1b:b9:d7:ce:26 1
00:1b:b9:da:de:65 1
********************************************************************************
希望达到的目的:现在想把rs.getString(1)的值00:0c:f7:fb:02:18输入到同一个库的另外一个表中stumermac
他的结构是
mysql> desc stumermac;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| time | time | YES | | NULL | |
| mac | varchar(17) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+所以还要
1 添加的id
2 添加当前插入的时间time
最后当然还有mac------rs.getString(1)的值00:0c:f7:fb:02:18
id如果为自增字段,可以不写:insert into table as select(time,mac1) from broadcast group by mac1
time为固定值,就是系统的时间如:"insert into table as select("+Calendar.getInstance().getTime()+",mac1) from broadcast group by mac1"
数据多的,用insert select 拼装sql,这里要注意主键的取值莫重复
数据量少的,就先select出要插入的值,再循环insert
insert into
表1
select
列1,列2,列3
from
表2方法2:
循环
insert into
表1
(
列1,列2,列3
)
values
(
value1,value2,value3
)
笨办法就是select一条就insert一条。