首页 > 代码库 > 软件工程综合实践心得(1)

软件工程综合实践心得(1)

软件工程综合实践心得(1

mysql 使用

  1.  sql 文的使用(增、删、改、查询)

创建数据库    mybatis

a1.  表的创建   保存的表名  userinfo

userid

int

11

 

 

选中

username

char

20

 

 

 

pwd

char

20

 

 

 

 

CREATE TABLE `userinfo` (

  `userid` int(11) NOT NULL auto_increment,

  `username` char(20) default NULL,

  `pwd` char(20) default NULL,

  PRIMARY KEY  (`userid`)

)

INSERT INTO `userinfo` VALUES (1, ‘Zhang‘, ‘211‘);

INSERT INTO `userinfo` VALUES (2, ‘Li‘, ‘3243‘);

INSERT INTO `userinfo` VALUES (3, ‘Liu‘, ‘252‘);

INSERT INTO `userinfo` VALUES (4, ‘Quan‘, ‘894‘);

INSERT INTO `userinfo` VALUES (5, ‘Yan‘, ‘3251‘);

INSERT INTO `userinfo` VALUES (6, ‘Wu‘, ‘87589‘);

INSERT INTO `userinfo` VALUES (7, ‘Pi‘, ‘911‘);

 

A2.查询

Select  *  from  userinfo    where  username = ‘Zhang’

 

 

 

package cn.neusoft.mybatis.jdbc;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class TestJdbc {

    public static void main(String[] args) {

      

       Connection con = null;

       PreparedStatement ps = null;

       ResultSet rs = null;    

       try {

           Class.forName("com.mysql.jdbc.Driver");

           con = DriverManager.getConnection(

                  "jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf-8",

                  "root",

                  "123");

           String sql = "select * from userinfo where username = ?";

           ps = con.prepareStatement(sql);?

           ps.setString(1, "Zhang");

           rs = ps.executeQuery();

           while(rs.next()){

              int userid = rs.getInt("userid");

              String username = rs.getString("username");

              String pwd = rs.getString("pwd");

             

              System.out.println("编号:"+userid+"用户名:"+username+"密码"+

              pwd);

           }

          

       } catch (ClassNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

}

 

软件工程综合实践心得(1)