1.MySQL常用命令
1.1 show databases; 显示现有的数据库
1.2 show tables; 显示当前数据库所包含的表
1.3 desc dept; 显示dept表的结构
1.4 select * from dept order by deptno desc limit 3,2;
dept表先按deptno降序排列,然后取出第三行后面两行的数据
1.5 select now(); 取出当前时间
1.6 select date_format(now(),'%d-%m-%y %h:%i:%s); 设置当前时间显示的格式
2.连接MySQL数据库
import java.sql.*;
public class TestMysqlConnection {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
System.out.println("SQLException:" + ex.getMessage());
System.out.println("SQLState:" + ex.getSQLState());
System.out.println("VendorError:" + ex.getErrorCode());
}
}
}