现在的位置: 首页 > 综合 > 正文

自学mysql数据库以及连接java

2013年09月20日 ⁄ 综合 ⁄ 共 4201字 ⁄ 字号 评论关闭

mysql关键字使用:

放弃正在输入的命令:\c

退出sql程序:\q

查看mysql服务器状态信息:\s

域完整性:主键primary key;  外键foreign key;

建立一个数据库:create database 库名;

创建表及列名:create table 表名(
   列名1 数据类型 约束,
   列名2 数据类型 约束,         
                 列名3 数据类型 约束,
          例:
                        ->id int(10) not null primary key,
                        ->name varchar(20) not null); 

 unsigned 不允许空间出现负数,可以使空间增加一倍,只能用于数值型
  例如
   id int(10) unsigned  not null primary key auto_increment
 主键及自增
  id int(10) not null primary key auto_increment

 向名中增加字段
  alert table 表名 add 字段名  类型 其他
 修改字段类型
  alter table 表名 change 列名 列名 类型
 更改列名
  alter table 表名 change 原列名  更改后的列名

 查看字段
  desc 表名
清空表: delete from 表名;

删除表:drop table [if exists] 表名;

删除数据库:drop database [if exists] 库名;

向表中添加一条记录:insert into 表名(列名1,列名2)values(**,**);

向表中添加多条记录:insert into <表名>(列名1,列名2)
                    values(**,**),
             (**,**),
             (**,**),
                    (**,**);

更改记录操作:update 表名 set 列名=更新值 where 更新条件;
             例如:update student set sname="Tom" where sname="Alex";

删除记录操作:delete from 表名 where 删除条件
      例如:delete from student where sage<18;

查询记录操作:select 列名 from 表名 where 查询条件 order by 列名 asc/desc;
             例如:select * from student;

使用集函数:count(列名)  计算元素的个数
            sun(列名)   对某一列值求和,但必须是整数
            avg(列名)  对某一列的值计算平均值
            max(列名) 找出某一列的最大值
            min(列名) 找出某一列的最小值
         例如:1.查询学生总数
                 select count(*) from student;
               2.查询选修课程的学生人数
                 select count(distinct studentid) from sc;
               3.查询1号课程的学生平均成绩
                 select avg(grade) from sc where courseid=1;
               4.查询1号课程的学生最高分和最低分
                 select max(grade) as '最高分',min(grade) as '最低分'
                   from sc where courseid=1;
               5.查询每个学生的平均成绩
                 select studentid,avg(grade) as '平均成绩' from sc
                    group by studentid ;
               6.查询学生的平均成绩在70分以上的
                 select studentid,avg(grade) as '平均成绩' from sc
                    group by studentid having avg(grade)>70;
          

 

 

、、、、、、、、、、、连接数据库

 

/**
 *
 */
import java.sql.*;
/**
 * @author Administrator
 *
 */
public class kk {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO 自动生成方法存根
  try {
   Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/myschool","root","123456");
   Statement stmt=conn.createStatement();
   ResultSet rs=stmt.executeQuery("select * from user");
   while (rs.next()) {
    int id=rs.getInt("id");
    String name=rs.getString("name");
    int score=rs.getInt("score");
    System.out.println("学号:"+id+",姓名:"+name+",分数"+score);
   }
  } catch (Exception e) {
   // TODO: handle exception
   System.out.println("故障"+e.toString());
  }
 }

}

 

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Login.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
     <center>
      <form action="Mainbody.jsp",method="post">
       <label>用户名:</label><input type="text" name="user" />
       <label>密码:</label><input type="password" name="pwd" />
       <input type="submit" value="登录" />
      </form>
    </center>
     <%
       
       String user1="";
       String pwd1="";
       try{
       String users=request.getParameter("user");
       String pwds=request.getParameter("pwd");
       
       Class.forName("org.gjt.mm.mysql.Driver");
       Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/myqq","root","123456");
       Statement stmt=conn.createStatement();
       ResultSet rs=stmt.executeQuery("select * from stu");
       while(rs.next()){
        user1=rs.getString("user");
        pwd1=rs.getString("pwd");
       }
       if(!users.equals(user1)||!pwds.equals(pwd1)){
        out.print("请输入正确的用户名或密码");
       }
       if(users.equals(user1)&&pwds.equals(pwd1)){
        response.sendRedirect("Mainbody.jsp");
       }
       
       }catch(Exception ex){
     System.out.print("系统错误"+ex.getMessage());       
       }
       
       
       
      %>
  </body>
</html>

抱歉!评论已关闭.