how can i use hibernate to loging in a jsp page - mysql

im using hibernate with my jsp page and mySQL , i know just how to save a session like that :
<%Session hibernateSession = MyDB.HibernateUtil.currentSession(); Transaction tx = hibernateSession.beginTransaction();
Student std = new Student();
std.setUserName("David");
hibernateSession.save(std);%>
, but how can i selecte from a table and print it like in mysql select * from student wher userName = *** and how can i update ?
finaly can i use hibernate in Login ?

Refer this for complete HQL help: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html

Related

Join two database table zend framework 1.12

I have used two mysql database in our projects. one database is connected the basic user information and another database used to store the daily activities. Now need to combine two database tables .
fetch user daily activity with user information , then need to join with master databases.
I found the solution in in PHP. But i want the solution on zend framework 1.12 ?
I used multidb functionality used to fetch different action .
resources.multidb.tb.adapter = "pdo_mysql"
resources.multidb.tb.host = "localhost"
resources.multidb.tb.username = "root"
resources.multidb.tb.password = ""
resources.multidb.tb.dbname = "#####"
resources.multidb.tb.default = true
resources.multidb.pl.adapter = "pdo_mysql"
resources.multidb.pl.host = "localhost"
resources.multidb.pl.username = "root"
resources.multidb.pl.password = ""
resources.multidb.pl.dbname = "#######"
But I want to query for join 2 tables in different databases.
example
SELECT db1.table1.somefield, db2.table1.somefield FROM db1.table1
INNER JOIN db2.table1 ON db1.table1.someid = db2.table1.someid WHERE
db1.table1.somefield = 'queryCrit';
Having in mind Zend's Join Inner declaration:
public function joinInner($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
And being '$this', for example, a Zend_Db_Table_Abstract implementation with adapter set to db1 (with _setAdapter()) and schema to "#####" (this is not really necessary because it'll use it as default):
$select = $this->select(true)->setIntegrityCheck(false)
->from(array('t1'=>'table1'),array('somefield')
->joinInner(array('t1b'=>'table1'),
't1.someid = t1b.someid',
array('t1b.somefield'),
'######')
->where('t1.somefield = ?', $queryCrit);
Please, note the the fourth parameter of the Inner Join method.
Hope this helps.

Cakephp 3 Auth Component $this->Auth->password() not working

i want to update my password with users table and in cakephp 2.0 i am using like that --
$haspass = $this->Auth->password($pass);
working fine but in cakephp3 it`s not working..
finally i have found the solution --
In Controller -- Use Like That
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
$password = $_POST['new_pswd'];
$hashPswdObj = new DefaultPasswordHasher;
$hashpswd = $hashPswdObj->hash($password); // it will hash the password

Yii2 - Connect to database inside Controller Action

Greetings,
Facts:
Database named -> acastro
Table called -> contacto
Fields in table are -> id, nome, email
I making an Yii2 application, and need to connect a highcharts chart to a table field in my database.
How can i inside an action called actionAdmin connect to my database and then count the number of id's in my Contacto table stored inside acastro database.
In the old Yii1.xx i used to establish connection this way:
public function actionAdmin() {
$sql = Yii::app()->db->createCommand('
SELECT count(*) as total
FROM contacto
')->queryAll();
$total = array();
for ($i = 0; $i < sizeof($sql); $i++){
$total[] = (int) $sql[$i]["total"];
}
$this->render('admin', array('total' => $total));
}
}
The problem is that this syntax no longer works in Yii2, and i've tried the sintaxe explained in Yii2 api guide but it always give's me error of undefined variable. Here is the code that i'm using to connect acording to Yii2 api guide:
use yii\db\Command;
$total = $connection->createCommand('SELECT count (*) FROM contacto')->queryAll();
What am i doing wrong ? Any solutions ?
Many thanks in advance.
I am not very sure that it will solve ur problem.
But in yii2 this the syntax
use app\models\Contacto; //look your Contacto Model namespace
$query = (new Query())->from('contacto');
$count = $query->count('column_name');
I hope this will help
The easiest syntax in Yii2 is:
$count=(new \yii\db\Query)->from('TBL_NAME')->count('*');
It just returns the count. For example: 500

Django how to select foreign value in query

Having following models:
User(models.Model):
...
login = ...
Asset(models.Model):
user = models.ForeignKey(User)
...
How to select users login in Asset query using django QuerySet capabilities. For example:
Asset.objects.extra(select = {'user_login' : 'user__login'})
make to return query set with user_login field in each model object
Each Asset object already has a foreign key to the user. So you can always access -
asset = Asset.objects.get(pk=any_id)
if asset.user.login == 'some_value':
do_some_magic()
Please read the documentation.
Use .select_related('user') to select all assets and related users in a single query. Then simply access it through asset.user.login.
assets = Asset.objects.selec_related('user').filter(<any filter>)
for asset in assets:
# no additional queries here, as the user objects are preloaded into memory
print asset.user.login
I have found following solution:
Asset.object.extra( select = {'user_login' : '`%s.%s`' % (User._meta.db_table, 'login') } ).order_by('user__login')
The order_by expression is used to make JOIN on User's model table, than user's login can be accessed in SELECT expression within user_table.login

Inserting data using Linq

I have a linq query to insert data in table. but its not working. I saw some example on internet tried to do like that but doesn't seems to work.
Tablename: login has 3 columns userid, username and password. I set userid as autoincrementing in database. So I have to insert only username and password everytime.Here's my code.
linq_testDataContext db = new linq_testDataContext();
login insert = new login();
insert.username = userNameString;
insert.Password = pwdString;
db.logins.Attach(insert);// tried to use Add but my intellisence is not showing me Add.I saw attach but dosent seems to work.
db.SubmitChanges();
have a look on http://www.codeproject.com/KB/linq/LINQToSQLBaseCRUDClass.aspx
linq_testDataContext db = new linq_testDataContext();
login insert = new login();
insert.username = userNameString;
insert.Password = pwdString;
db.logins. InsertOnSubmit(insert);
db.SubmitChanges();
If you Attach - It should attach to the particular object Context .But it wont reflect in database .If you want to insert any values try with InsertOnSubmit(object) and do
SubmitChanges() to save it in database
Attach() is the wrong method, you need to call InsertOnSubmit() to let Linq-To-Sql generate an insert statement for you. Attach() is for distributed scenarios, where your entity has not been retrieved via the same data-context that is used for submitting changes.
linq_testDataContext db = new linq_testDataContext();
login insert = new login();
insert.username = userNameString;
insert.Password = pwdString;
db.logins.InsertOnSubmit(insert);// tried to use Add but my intellisence is not showing me Add.I saw attach but dosent seems to work.
db.SubmitChanges();
Method to save employee details into Database.
Insert, Update & Delete in LINQ C#
Employee objEmp = new Employee();
// fields to be insert
objEmp.EmployeeName = "John";
objEmp.EmployeeAge = 21;
objEmp.EmployeeDesc = "Designer";
objEmp.EmployeeAddress = "Northampton";
objDataContext.Employees.InsertOnSubmit(objEmp);
// executes the commands to implement the changes to the database
objDataContext.SubmitChanges();