processing-MySQL learning1-getString() , getInt() and query()


import de.bezier.data.sql.*;

/*

http://bezier.de/processing/libs/sql/
http://wiki.processing.org/w/How_to_Install_a_Contributed_Library

*/

MySQL msql;
MySQL dbselect;
MySQL dbparam;

void setup()
{
    size( 100, 100 );
  
    // this example assumes that you are running the 
    // mysql server locally (on "localhost").
  
    String user     = "hui1570";
    String pass     = "04263028";
    String database = "mydatas";

    //connect MySQL1 to database
    msql = new MySQL( this, "localhost", database, user, pass );
    if ( msql.connect() )
    {
        msql.query( "SELECT COUNT(*) FROM user" );
        msql.next();
        
        //print the number of rows that are queryed
        println( "number of rows: " + msql.getInt(1) );
    }
    else
    {
        // connection failed !
    }
    
    println("======================================================" );
    
     //connect MySQL2 to database
    dbselect = new MySQL( this, "localhost", database, user, pass );    
    if ( dbselect.connect() )
    {
        // now read it back out
        //we only can display three rows of user because :limit 3
        dbselect.query( "SELECT * FROM user limit 3" );
        
        while (dbselect.next())
        {
            String s = dbselect.getString("username");
            int n = dbselect.getInt("id");
            println(s + "   " + n);
        }
    }
    else
    {
        // connection failed !
    }    
    
    println("======================================================" );
    
    //connect MySQL3 to database
    dbparam = new MySQL( this, "localhost", database, user, pass ); 
    //setDebug(boolean YesNo): Turn some debugging on/off.
    dbparam.setDebug(false);

    if ( dbparam.connect() )
    {      
       //% on behalf of any number of characters
       String p1="us%";
       String p2="950%";
       String sqlstring = "SELECT * FROM user where username like '%s' and password like '%s' and id < %d order by id limit %d";
       //String sqlstring = "SELECT * FROM user where username like '%s' and id < %d order by id limit %d and password like '%s'";  Why it is wrong ??????
        
       //deliver all parameters to sqlstring: %s==p1 ; %s=p2 ; %d==10 ; %d==3  
       dbparam.query(sqlstring , p1, p2, 10, 3 );

      while (dbparam.next())
      {
          String s = dbparam.getString("username");
          int n = dbparam.getInt("id");
          String p=dbparam.getString("password");
          println(s + "   " + n+"   "+p);
      }
    }    
    else
    {
        // connection failed !
    }   
}

void draw()
{
    // i know this is not really a visual sketch ...
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值