import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JDBCTest {
public static void main(String[] args) throws SQLException{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8", "root", "yang");
Statement st = conn.createStatement();
//执行事务
int j = st.executeUpdate("INSERT INTO EMP VALUES (55,'yang','22')");
//执行查询
ResultSet rs= st.executeQuery("SELECT * FROM EMP");
// System.out.println("i:"+"rs:"+rs.getRow());
//得到结果集(rs)的结构信息,比如字段数、字段名等
ResultSetMetaData md = rs.getMetaData();
//返回此 ResultSet 对象中的列数
int columnCount = md.getColumnCount();
List list = new ArrayList();
Map rowData = new HashMap();
//ResultSet 光标最初位于第一行之前;
//rs.next() 光标向下移动一行,得到每一列的值
while (rs.next()) {
System.out.println("rs:" + rs.getRow());
rowData = new HashMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
System.out.println("list:" + list.toString());
conn.close();
}
}
1 jdbc链接改成单例
2 改成配置文件 properties
3 查询出来的值放入对象中
4 新增和查询等方法 写入到新方法中
其它~~~