Luh lan lara
Kesalahan bisa kedadeyan nalika mbukak program Java. Nalika nggarap database, kesalahan bakal kedadeyan. Iku kabeh babagan endi sing bisa diprediksi lan menehi solusi sing cukup.
Klompok gedhe pisanan kesalahan bakal nunggu sampeyan nalika nglakokake baris iki:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
Apa sampeyan bisa nyana kene?
Error 1. Driver not found .
Yen sampeyan entuk kesalahan "Ora ana driver sing cocog kanggo ..." , tegese DriverManager ora ngerti jinis DBMS sing ana ing mburi URL sampeyan. Contone, sampeyan nulis jdbc_mysql: tinimbang jdbc:mysql:
Kesalahan 2. Kesalahan sambungan .
Yen sampeyan nggawe kesalahan ing jeneng host, sampeyan bakal entuk pesen kaya "Ora ana host sing dingerteni" utawa "Gagal link komunikasi".
Kesalahan 3. Jeneng database salah .
Yen sampeyan salah nulis jeneng database utawa nyambung menyang server liyane sing ora ana, sampeyan bakal entuk pesen kaya "Database ora dingerteni 'supershop3'" .
Kesalahan 4. Salah mlebu utawa sandhi .
Yen sampeyan kelalen sandhi database utawa salah ngetik, mesthine sampeyan bakal nampa pesen kaya "Akses ditolak kanggo pangguna 'root'@'localhost' (nggunakake sandhi: YES)" .
Pangecualian SQL
Yen sambungan menyang pangkalan tetep ditetepake, mula bakal luwih menarik. Yen ana kesalahan nalika nggarap database, JDBC nduweni pangecualian khusus - java.sql.SQLException . Uga sawetara varieties sawijining.
Istiméwa iki mung siji cara tambahan (dibandhingake kelas Exception) - getSQLState () cara, kang ngasilake kode status (string) sing SQL server bali menyang. Penanganan kesalahan katon kaya iki:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
try {
int rowsCount = statement.executeUpdate("DELETE FROM ‘Unemployees’");
} catch (SQLException ex) {
// If table doesn't exist
if (ex.getSQLState().compareTo("X0Y32") != 0) {
throw ex;
}
} finally {
connection.close();
}
Ana atusan kode kesalahan. Sampeyan bisa ndeleng dhaptar lengkap kene .
Nanging kadhangkala kode kesalahan mbantu JDBC ngerti kesalahan sing luwih apik lan banjur ora mung SQLException, nanging SQLException khusus:
BatchUpdateException | Kesalahan nalika njaluk grup |
DataTruncation | Asring kedadeyan nalika ngethok data sing dawa |
SQLClientInfoException | Klien ngliwati paramèter sing ora bisa disetel ing sambungan: Sambungan |
SQLDataException | Kesalahan karo data, rincian gumantung saka jinis DBMS |
SQLException | Kesalahan akses database utawa kesalahan umum liyane |
SQLFeatureNotSupportedException | DBMS ora ndhukung fungsi iki |
SQLIntegrityConstraintViolationException | Kabeh kesalahan karo kanggo SQLState ==22 |
SQLInvalidAuthorizationSpecException | Akses lan/utawa kesalahan wewenang |
SQLNonTransientConnectionException | Kabeh kesalahan karo kanggo SQLState ==08 |
SQLRecoverableException | Ana kesalahan, nanging bisa didandani kanthi intervensi aplikasi |
SQLSyntaxErrorException | Kesalahan ing sintaks pitakon |
SQLTimeoutException | Panjaluk njupuk dawa banget |
SQLTransactionRollbackException | Kesalahan nalika bali transaksi |
SQLWarning | Warning ditanggepi dening DBMS |
Conto kesalahan
Yen ana kesalahan ing tataran aplikasi server, biasane mung bisa dicathet banjur ditangani kanthi luwih rinci. Nanging yen kedaden nalika aplikasi lagi mlaku ing komputer programmer, sampeyan kudu kasebut kanthi teliti, neliti alesan kanggo iki.
Ana rong kelas kesalahan paling gedhe nalika nggarap database:
- Njaluk kesalahan teks
- Kesalahan nalika nggarap ResultSet
Kesalahan ing teks panyuwunan bisa kedadeyan asring banget. Pitakonan bisa dawa banget, ngemot sawetara tabel (digabungake liwat JOIN) lan subkueri (SUBSELECT). Ora gampang nemokake kesalahan ing panjaluk kasebut. Kajaba iku, panjalukan asring digabungake saka bagean, data ditambahake ing kana.
Kesalahan pisanan sing kudu dingerteni yaiku SQLSyntaxErrorException . Kesalahan kasebut biasane tegese sampeyan duwe salah ketik ing awak panyuwunan.
Ayo njupuk conto favorit kita:
ResultSet results = statement.executeQuery("SELECT Count(*) FROM user");
results.next();
int count = results.getInt(1);
Lan "ora sengaja" mbusak tanda bintang ing awak panyuwunan:
ResultSet results = statement.executeQuery("SELECT Count() FROM user");
results.next();
int count = results.getInt(1);
Banjur kita bakal entuk pangecualian:
Exception in thread "main" java.sql.SQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') FROM task' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
Server SQL ngandhani yen ana kesalahan sintaks sadurunge FROM.
Kita kasebut kanthi teliti, katon ing panjalukan lan mikir. Yen ora ana apa-apa, lan iki kedadeyan asring banget, mula sampeyan kudu takon wong kanggo ndeleng apa sing salah.
Kesalahan populer liyane yaiku karya sing salah karo ResultSet - asil panyuwunan. Upamane sampeyan kelalen sadurunge maca data, "baris saiki" obyek ResultSet sadurunge baris pisanan, sampeyan bakal duwe kode ing ngisor iki:
ResultSet results = statement.executeQuery("SELECT Count(*) FROM user");
// results.next();
int count = results.getInt(1);
Sampeyan bakal entuk kesalahan iki:
2012 12:55:48 AM jButton5ActionPerformed
SEVERE: null
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5650)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
Kita kasebut kanthi teliti, katon ing kesalahan lan mikir. Banjur kita google lan golek sawetara conto lan nyoba kanggo mangerteni solusi.