您的位置:首页 > 产品设计 > UI/UE

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

2015-12-18 16:59 543 查看






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 ...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: