try {
//加载这个mysql,jdbc驱动的时候需要引用的jar包是
//mysql-connector-java-5.1.7-bin.jar
//下载地址为:http://www.pc6.com/softview/SoftView_99415.html
Class.forName("com.mysql.jdbc.Driver");System.out.println("Success loading Mysql Driver!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //加载MYSQL JDBC驱动程序
Connection connect = DriverManager.getConnection(
"jdbc:mysql://192.168.120.96:3306/dbwww58com_av-12xxxx?useUnicode=true&characterEncoding=UTF-8&connectTimeout=1000","wxtest","12345678");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from t_shangji where userid = 1");
//user 为你表的名称
while (rs.next()) {
//打印出查询到的表的字段。取出来的数据格式转换了,在ResultSet实体中是看不到对应的字段的。
System.out.println(rs.getString("content"));System.out.println(rs.getString("modifytime"));
}
1.单表查询
SELECT job,SUM(sal) sum //选择需要查询的字段
FROM emp //需要查询的表
WHERE job<>'SALESMAN' //非这个字段
GROUP BY job //通过job分组
HAVING SUM(sal)>5000 //查询总和sal>5000的job
ORDER BY sum ASC; //排序方法
2.多表查询
SELECT d.deptno,d.dname,d.loc,COUNT(e.empno) mycount,NVL(AVG(e.sal),0) myavg
FROM dept d,emp e
WHERE d.deptno=e.deptno(+)
GROUP BY d.deptno,d.dname,d.loc
HAVING AVG(sal)>2000;
3.子查询
SELECT d.deptno,d.dname,d.loc,temp.count,temp.avg
FROM dept d,(
SELECT deptno dno,COUNT(empno) count,AVG(sal) avg
FROM emp
GROUP BY deptno) temp
WHERE d.deptno=temp.dno(+);