Jump to content

Recommended Posts

Posted

Γεια σε όλους είχα κάνει παρόμοιο τόπικ πριν πολύ καιρό αλλά μιας και τώρα καταλαβαίνω τα πράγματα 1000 φορές καλύτερα είπα να ξανακάνω, άλλωστε αυτό θα περιέχει πιο πολλά πράγματα. Σήμερα θα μιλήσουμε συγκεκριμένα για το πώς μπορούμε να κάνουμε συνδέσεις στην database μέσω Java στο L2J και πώς μπορούμε να εκτελούμε εντολές και όλα τα σχετικά

 

Πότε και πού χρησιμοποιούμε SQL connections, σε τι είναι χρήσιμες?

Σίγουρα κάποιος πρώτη φορά θα το αναρωτηθεί. Λοιπόν καταρχάς συνδέσεις με database θα χρειαστόυμε σε περιπτώσεις που θέλουμε να πάρουμε στοιχεία απο την database (λογικό) από οποιοδήποτε table,column θέλουμε εμείς και σε περιπτώσεις που θέλουμε κάτι να περάσουμε μέσα στην database μας. Το καλό πράγμα σε όλη την ιστορία που χωρίς αυτό δεν θα είχε και νόημα,είναι ότι στην database τα πράγματα μας μένουν και μετά το restart του server μας , μένουν για πάντα αν το θελήσουμε! (ανάλογα βέβαια) Χαρακτηριστικά Παραδείγματα : donate system , nobless , pvp kills.

Και τα 3 παρατηρείτε ότι δεν διαλύονται με το rr του server , ένας donator είναι και μετά το rr donator, ένας nobless είναι Nobless και μετά το rr και ένας παίκτης μετά το rr του server έχει όσα pvp είχε πριν, δεν μηδενίζονται.

 

Πολλά λόγια , πάμε στο ψητό , ας αρχίσουμε

Θα προσπαθήσω να το εξηγήσω όσο καλύτερα μπορώ.

Καταρχάς κάνουμε Import για αρχή τα εξής

import java.sql.Connection;
import java.sql.PreparedStatement;
import net.sf.l2j.L2DatabaseFactory;

Όπως βλέπετε έχουμε κάνει Import την κλαση Connection και την κλάση PreparedStatement .

κλάση Connection: με αυτή θα δημιουργήσουμε την σύνδεση μας για να μπούμε στην database του server, το λέει και η λέξη , έτσι δεν είναι?(Connection=σύνδεση)

κλάση PreparedStatement: στην SQL εκτελόυμε συνέχεια statements ( προτάσεις - εντολές ).

π.χ ΚΑΝΕ ΟΛΟΥΣ ΤΟΥΣ ΠΕΚΤΕΣ ΝΟΒΛΕΣΣ

Αυτή είναι μια πρόταση που εκτελείται , βέβαια όχι έτσι αλλά στην SQL για να το κάνουμε αυτό γράφουμε

UPDATE characters SET nobless=1 WHERE accesslevel=0

πχ έτσι κάνουμε nobl τους παίκτες που δεν ειναι GM

Άρα αυτή η κλάση μας βοηθάει σε αυτό , να δημιουργήσουμε προτάσεις σε SQL μέσω Java.

 

Αφού πλέον γνωρίζουμε σε τι θα μας χρειαστούν αυτές οι 2 κλάσεις πάμε να τις χρησιμοποιήσουμε.

Καταρχάς δημιουργούμε ένα αντικείμενο για την καθεμία κλάση βάζοντας το τιμή null προς το παρόν δηλαδή άδεια τιμή , δεν θα περιέχει τίποτα.

Connection connection = null;
PreparedStatement state = null;

Τώρα καταλαβαίνετε και οι ίδιοι σας ότι η σύνδεση είναι δυνατόν να μην πετύχει 100% ειδικά αν έχει και προτάσεις μέσα λόγω λαθών στην σύνταξη του SQL ή ακόμα λόγω της μη-ύπαρξης της database άρα θα χρειαστούμε να περυκυκλώσουμε τον κώδικα μας με try/catch. Τα προσθέτουμε λοιπόν

try
   {
   }
catch(Exception e)
{
e.printStackTrace();
}

μέσα στα blocks του try θα είναι ο κύριος code. στο catch βλέπουμε πως βάλαμε την μέθοδο e.printStackTrace(); η οποία είναι καλή να την βάζετε γιατί αν εμφανιστεί οποιοδήποτε error θα σας το εμφανίζει στο GS και θα είναι πιο εύκολο να το fix.

Πάμε λοιπόν να δώσουμε ζωή στα αντικείμενα που δημιουργήσαμε νωρίτερα και μη ξεχάσαμε απο δω και πέρα ότι κάνουμε είναι μέσα στο try . Καταρχάς λέμε πως το αντικείμενο connection θα είναι μια σύνδεση η οποία συνδέεται με την database που τρέχει ο server, για να το κάνουμε αυτό γράφουμε

connection = L2DatabaseFactory.getInstance().getConnection();

Αυτό μονάχα,πλέον έχουμε σύνδεση στην database χρησιμοποιώντας το connection.Τώρα θα μπορούσαμε να κλείσουμε τη σύνδεση κάλλιστα , αλλά κανένας ο λόγος να δημιουργήσουμε σύνδεση χωρίς ουσιαστικό λόγο. Ήρθε η ώρα να χρησιμοποιήσουμε το άλλο μας αντικείμενο, το state . Αυτό απλά είναι αυτο που είπαμε, θα το κάνουμε να παίρνει μια πρόταση την οποία θα εκτελέσουμε αργότερα στην database . Ας πούμε ότι θέλω να κανω το name σε όλους τους GM, ADMINISTRATOR. Άχρηστο φυσικά γιατί μπορούμε να το κάνουμε και χωρίς connection(και πάλι άχρηστο) στην database αλλά είναι ένα παράδειγμα για να δείτε πως δουλεύει , γράφουμε

state = connection.prepareStatement("UPDATE characters SET char_name='ADMINISTRATOR' WHERE accesslevel>0");

Το update το χρησιμοποιούμε για να αλλάξουμε τιμές στην database σε υπάρχον πράγματα , εδώ αλλάξαμε το ονομα τον GMS.

Έτοιμο και το state , τώρα τι μένει? να εκτελέσουμε τη πρόταση(το state δηλαδή πλέον).

άρα έχουμε

state.execute();

το εκτελέσαμε , αυτό ήταν. και τώρα αυτό που δεν ξεχνάμε ποτέ να κάνουμε είναι να κλείσουμε τη σήνδεση αλλά και την πρόταση αλλιώς θα έχουμε έρρορς άρα πολύ απλά γράφουμε

connection.close();
state.close();

Αν βρει καποιο έρρορ παει κατευθείαν στο catch , τώρα προσθέτουμε και το εξής

finally
{
	  try
			{
			connection.close();
			}
		catch (SQLException e)
			{

			e.printStackTrace();
			}
		      }

Με το finally λέμε τι θα κάνει τελικά και αυτό που θέλουμε να κάνει τελικά στα σίγουρα , ειναι να κλείνει την σύνδεση , άρα έτσι εξηγείται το παραπάνω.

Αυτό ήταν ολόκληρος ο κώδικας θα είναι έτσι

Connection connection = null;
PreparedStatement state = null;
try
   {
       connection = L2DatabaseFactory.getInstance().getConnection();
      state = connection.prepareStatement("UPDATE characters SET char_name='ADMINISTRATOR' WHERE accesslevel>0");
      state.execute();
      connection.close();
      state.close();
   }
catch(Exception e)
{
e.printStackTrace();
}
finally
{
	  try
			{
			connection.close();
			}
		catch (Exception e)
			{

			e.printStackTrace();
			}
		      }

Αυτό ήταν ένα καθαρό εύκολο παράδειγμα για το πως ανανεώνουμε (UPDATE) υπάρχον στοιχεία στην database μας. Τώρα πάμε να δούμε πώς θα διαβάζουμε απο την database

Στο παράδειγμα θα διαβάσουμε πόσα pvp έχει ένας παίκτης με ονομα Unix για παράδειγμα.

αρχίζουμε πάλι το ίδιο γράφουμε τα ίδια:

Connection connection = null;
PreparedStatement state = null;
try
   {
      
   }
catch(Exception e)
{
e.printStackTrace();
}
finally
{
	  try
			{
			connection.close();
			}
		catch (Exception e)
			{

			e.printStackTrace();
			}
		      }

Και τώρα μέσα στο try πάμε για τον κύριο code, δεν εξηγώ τι κάνει το καθένα τώρα τα είπα παραπάνω.

Δημιουργουμε το connection

connection = L2DatabaseFactory.getInstance().getConnection();

Και τώρα το κύριο που αλλάζει είναι η πρόταση , το state . γράφουμε

state = connection.prepareStatement("SELECT pvpkills FROM characters WHERE char_name='Unix'");

Έτσι διαλέγουμε ( select ) τα pvpkills απο το table characters(FROM characters) όπου ονομα ισούται με Unix(char_name='Unix')

Τώρα δεν θα εκτελέσουμε τον κώδικα γιατί δεν έχει ουσία δε θα συμβεί τίποτα.

Δημιουργούμε μια μεταβλητή int στην οποία θα αποθηκεύονται το πόσα pvp έχει.

int pvps = 0

Τώρα για να πάρουμε το αποτέλεσμα της πρότασης (state) και να το αποθηκεύσουμε στην μεταβλητη pvps θα χρειαστεί να κάνουμε import μια νέα κλάσση με όνομα ResultSet

import java.sql.ResultSet;

η κλάση αυτή μας βοηθάει να πάρουμε το εκτελέσιμο αποτέλεσμα , εδώ συγκεκριμένα τα pvpkills του παίκτη Unix.

Τώρα πάμε κάτω απο το αντικείμενο connection και δημιουργούμε και ένα αντικείμενο ResultSet με άδεια τιμή προς το παρόν

ResultSet rset = null;

Μετά πάμε εκεί που είχαμε μείνει και δίνουμε αξία στο αντικείμενο rset , γράφουμε

rset = state.executeQuery();

Τώρα δώσαμε τιμή στο rset, με αυτην την εντολή λέμε στο rset να δωθεί στην ουσία το αποτέλεσμα της πρότασης (state)

Ωραία , τώρα πως δίνουμε το αποτέλεσμα που είναι τα pvp kills στην μεταβλητη pvps?

Γράφουμε

while(rset.next())
{
pvps = rset.getInt("pvpkills");
}

Λιγο περίπλοκο ε?Είναι απλό .

με το while(rset.next()) λέμε ουσιαστικά όσο το αντικείμενο rset ( που ειναι το αποτελεσμα ) έχει κάτι να μας δώσει, έχει ένα αποτέλεσμα (στην ουσία έχει 1 γιατί μονο απο 1 παικτη παιρνουμε τα pvps)

τότε η μεταβλητη pvps να ισούται με την μεταβλητή pvpkills του rset , to pvpkills είναι αυτό που γράψαμε στην πρόταση μας. SELECT pvpkills , έτσι παίρνουμε την τιμή του.

άρα τώρα αυτό ηταν, η μεταβλητή pvps δηλώνει πόσα pvps έχει ο παίκτης Unix.

άρα λοιπόν τώρα κλείνουμε ότι πριν αλλά και το rset

connection.close();
rset.close();
state.close();

 

 

Αυτά ήταν κάτι βασικά που πρέπει να ξέρετε, διαβάστε τα κάντε εξάσκηση και δέστε παραδείγματα απο διάφορα codes που χρησιμοποιούν συνδέσεις στην database, σίγουρα θα καταλαβαίνετε πολλά περισσότερα  :D

Posted

Poly kalo guide

+oti tha prepei na kseroun na ftiaxnoun sql queries kai episis poly simantiko kalo tha itan ta queries na ta ftiaxnete kai na ta dokimazete stin navicat apo to na kanete compile kai na deite meta oti exete sintaktiko lathos.

Posted

Fobero guide. Gj UnixCode.

thanks bro

Poly kalo guide

+oti tha prepei na kseroun na ftiaxnoun sql queries kai episis poly simantiko kalo tha itan ta queries na ta ftiaxnete kai na ta dokimazete stin navicat apo to na kanete compile kai na deite meta oti exete sintaktiko lathos.

euxaristw

  • 1 year later...
Posted (edited)

Ευχαριστώ πάρα πολύ ρε αδελφέ! Τώρα μπορώ να φτιάχνω τους δικούς μου κώδικες γρήγορα και εύκολα με της γνώσεις που έχω στο java!

Edited by BadSystem™
Posted

gtp einai ta code, outdated

 

twra apla vazeis try( )

                          {

 

                          }

 

kai den xriazete na kaneis close se finally pou merikes fores petane npe

Posted

gtp einai ta code, outdated

 

twra apla vazeis try( )

                          {

 

                          }

 

kai den xriazete na kaneis close se finally pou merikes fores petane npe

malon den prosekses oti lei oti ftiaxtike martio tou 2012 tote etsi itane 

Posted

malon den prosekses oti lei oti ftiaxtike martio tou 2012 tote etsi itane 

 

to try with resources vgike me tin java7 to 2011 trele, dld ena xrono peripou prin to guide

Posted (edited)

gtp einai ta code, outdated

 

twra apla vazeis try( )

{

 

}

 

kai den xriazete na kaneis close se finally pou merikes fores petane npe

++ Edited by Nightw0lf
  • 3 weeks later...
Posted

oraios odigos file mou den to exw xanadei prosopika egw ... Tha ithela na kanw mia erotisi tha mporousa na xrisimopoiisw auto ton tropo gia admin panel event live , diladi na kanw event live mesa ston server mou ? h tha ypirxe problima lags k.t.l?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Posts

    • SOCNET — is a universal service combining a digital goods store, an SMM panel, and a Telegram bot for purchasing Telegram Stars; here you will find TikTok, Instagram, Reddit, Twitter, Telegram, Facebook, LinkedIn, WhatsApp, SnapChat, YouTube, Google, Discord accounts, emails (Outlook, Hotmail, Gmail, Rambler, Firstmail, and others), access to ChatGPT 5, gift cards, and premium subscriptions to many services — all at the best prices! We are currently actively looking for new suppliers for various product categories, which include: Priority list of positions: — Snapchat old and new accounts | With snapscores | Geo: Europe/USA | Full access via email/phone number — Reddit old accounts (brute or cracked origin, self-regs) with post and comment karma from 100 up to 100,000+ | Full access with email included — LinkedIn old accounts with real connections | Geo: Europe/USA | Full email access + active 2FA password — Instagram old accounts (2010-2023) | Full email access (possibly with attached 2FA password) — Facebook old accounts (2010-2023) | Full email access (possibly with attached 2FA password) | With friends or without friends | Geo: Europe/USA/Asia — Threads accounts | Full email access (possibly with attached 2FA password) — TikTok/Facebook/Google ADS Agency advertising accounts We are also open to considering other product categories from you, feel free to contact us through the details listed below in this thread! ⚡ Terms of cooperation: ⚡ 1. You prepare a preliminary description of your product 2. We publish your product in our online store and Telegram bots (in both Russian and English). 3. After the product is sold in our stores, we transfer the funds to you by any convenient method within 24 hours from the moment of sale (any cryptocurrency, PayPal, Payeer, TG Stars, and other methods)   ‼ If you have doubts about working with us or are afraid to send the product first, we agree to work through an escrow service of a trusted provider, or you can simply check out the huge base of reviews about our services in this Google document (reviews from our website, Telegram bot, and other platforms where our products are listed). Document ⭐ We invite you to COOPERATE and EARN with us ⭐ Do you want to sell your product or service in our stores and earn money? Become our partner or propose mutually beneficial cooperation? Become our wholesale client and receive the best conditions and discounts? You can contact us through the CONTACTS listed in this thread. We build a reliable partnership network in all areas of online business. We are ready to participate in joint content creation, giveaways, and events! If you work in digital, traffic, accounts, or media and other fields — let’s discuss possible cooperation. We value quality, transparency, and growth. If this resonates with you — write to us, we are open to dialogue! Our current list of partners ⭐ Our contacts ⭐ ➡ Telegram ➡ WhatsApp ➡ Email: solomonbog@socnet.store ➡ Discord: socnet_support ⭐ Our online store ⭐ SOCNET.STORE ⭐ Our Telegram bot for purchasing Telegram Stars ⭐ SOCNET.CC ⭐ Our SMM Panel for social media promotion ⭐ SOCNET.PRO ⭐ Telegram shop ⭐ SOCNET.SHOP   ✅ News resources ➡ Telegram channel ➡ Discord server ➡ WhatsApp channel
    • SOCNET — is a universal service combining a digital goods store, an SMM panel, and a Telegram bot for purchasing Telegram Stars; here you will find TikTok, Instagram, Reddit, Twitter, Telegram, Facebook, LinkedIn, WhatsApp, SnapChat, YouTube, Google, Discord accounts, emails (Outlook, Hotmail, Gmail, Rambler, Firstmail, and others), access to ChatGPT 5, gift cards, and premium subscriptions to many services — all at the best prices! We are currently actively looking for new suppliers for various product categories, which include: Priority list of positions: — Snapchat old and new accounts | With snapscores | Geo: Europe/USA | Full access via email/phone number — Reddit old accounts (brute or cracked origin, self-regs) with post and comment karma from 100 up to 100,000+ | Full access with email included — LinkedIn old accounts with real connections | Geo: Europe/USA | Full email access + active 2FA password — Instagram old accounts (2010-2023) | Full email access (possibly with attached 2FA password) — Facebook old accounts (2010-2023) | Full email access (possibly with attached 2FA password) | With friends or without friends | Geo: Europe/USA/Asia — Threads accounts | Full email access (possibly with attached 2FA password) — TikTok/Facebook/Google ADS Agency advertising accounts We are also open to considering other product categories from you, feel free to contact us through the details listed below in this thread! ⚡ Terms of cooperation: ⚡ 1. You prepare a preliminary description of your product 2. We publish your product in our online store and Telegram bots (in both Russian and English). 3. After the product is sold in our stores, we transfer the funds to you by any convenient method within 24 hours from the moment of sale (any cryptocurrency, PayPal, Payeer, TG Stars, and other methods)   ‼ If you have doubts about working with us or are afraid to send the product first, we agree to work through an escrow service of a trusted provider, or you can simply check out the huge base of reviews about our services in this Google document (reviews from our website, Telegram bot, and other platforms where our products are listed). Document ⭐ We invite you to COOPERATE and EARN with us ⭐ Do you want to sell your product or service in our stores and earn money? Become our partner or propose mutually beneficial cooperation? Become our wholesale client and receive the best conditions and discounts? You can contact us through the CONTACTS listed in this thread. We build a reliable partnership network in all areas of online business. We are ready to participate in joint content creation, giveaways, and events! If you work in digital, traffic, accounts, or media and other fields — let’s discuss possible cooperation. We value quality, transparency, and growth. If this resonates with you — write to us, we are open to dialogue! Our current list of partners ⭐ Our contacts ⭐ ➡ Telegram ➡ WhatsApp ➡ Email: solomonbog@socnet.store ➡ Discord: socnet_support ⭐ Our online store ⭐ SOCNET.STORE ⭐ Our Telegram bot for purchasing Telegram Stars ⭐ SOCNET.CC ⭐ Our SMM Panel for social media promotion ⭐ SOCNET.PRO ⭐ Telegram shop ⭐ SOCNET.SHOP   ✅ News resources ➡ Telegram channel ➡ Discord server ➡ WhatsApp channel
    • sell adena, items, account l2rebon signature x1 - 1kk = 1 dollars l2reborn x10 - 500kk = 4 dollars E-Global x Lu4 - 1kk = 2 dollars BOHPTS - x20-x500 TOP PRICE DISCORD - GODDARDSHOP TELEGRAM - MMOPROMO THE BEST PRICES IN THE WORLD OF LINEAGE 2
    • Can you reupload system patch?
    • he was my customer many years ago, i setup his acp and some statistics on his website, plus some security it is big difference than "friend"   its more dissapointment than hate, take example acis, how many years still development. PS: you know i was selling websites and i still sell donate panels for l2 servers right?
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock