THE_CHOSEN_ONE
1903-2012
- Член од
- 13 август 2007
- Мислења
- 3.724
- Поени од реакции
- 1.333
Sakam da napravam dekstop aplikacija vo java.
Prashanjeto mi e, koja baza e najdobra za java?
Prashanjeto mi e, koja baza e najdobra za java?
За Java Desktop апликација според мене најдобра база е: Apache Derby .Sakam da napravam dekstop aplikacija vo java.
Prashanjeto mi e, koja baza e najdobra za java?
package formi;
import java.sql.*;
public class vtoraforma {
public static void main (String[ ] args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/test";
String userName = "root";
String password = "admin";
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance ( );
conn = DriverManager.getConnection (url, userName, password);
// System.out.println ("Konektirano");
Statement s = conn.createStatement ( );
s.executeQuery ("SELECT * FROM akaunti");
ResultSet rs = s.getResultSet ( );
int count = 0;
while (rs.next ( )) // loop through rows of result set
{
int id = rs.getInt (1);
String ime = rs.getString(2);
String prezime = rs.getString(3);
String drzava = rs.getString(4);
++count;
System.out.println (count + ",id: " + id+ ",ime: " +
ime + ",prezime: " + prezime + ",drzava: " + drzava );
}
rs.close ( ); // close result set
s.close ( ); // close statement
System.out.println (count + " zapisi se vrateni);
}
catch (Exception e)
{
System.err.println ("Nemoze da se konektira"+e);
}
finally
{
if (conn != null)
{
try
{
conn.close ( );
// System.out.println ("Diskonektirano"); /* debagiranje */
}
catch (Exception e) { /* ignoriraj gi greskite */ }
}
}
}
}
Ти сакаш да се поврзиш со MySQL база, но ти фали MySQL Connector/J . Него можеш да го симниш од http://www.mysql.com/products/connector/j/Pak jas
Eve go kodot:
...
Ja dobivam ovaa greska:
Cannot connect to serverjava.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Не знам. Но мислам дека „поглавните“ команди како креирање база, табела, додавање запис, SELECT,... се исти.THE_CHOSEN_ONE напиша:Dime koja e razlikata pomegu sql komandite pri rabota so Derby i so MySql?
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
public class NewJFrame3 extends javax.swing.JFrame {
/** Creates new form NewJFrame3 */
public NewJFrame3() {
initComponents();
ActionListener slushac=new OkSlushac();
jButton1.addActionListener(slushac);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
kodot e izostaven poradi preglednost
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame3().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField10;
private javax.swing.JTextField jTextField11;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField8;
private javax.swing.JTextField jTextField9;
// End of variables declaration
private class OkSlushac implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] niza;
niza=new String[10];
int i;
for (i = 1; i <= 10; i++) {
niza[i]="jTextField"+i.getText();
}
System.out.println("Inserting values in Mysql database table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "dental";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","admin");
try{
Statement st = con.createStatement();
String sql;
String sql1;
String sql2;
sql="insert into akaunti values {";
for (i = 0; i < 10; i++) {
sql1+="'"+niza[i]+"',";
}
sql2=sql+sql1+")";
int val = st.executeUpdate(sql2);
System.out.println("1 row affected");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
Pak jas.
Napraviv ednostavana forma so 10 Text Fields i Button ,inputot vo Text Fildovite sakam da bide zapishan vo tabelata akaunti pri klikot na Buttonot.
Ja dobivam slednava greska:
NewJFrame3.java:171: int cannot be dereferenced
niza="jTextField"+i.getText();
Mislam deka znam otprilika sto e problemot ali ke cekam odgovor od ekspertite.
...
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
public class NewJFrame3 extends javax.swing.JFrame {
/** Creates new form NewJFrame3 */
public NewJFrame3() {
initComponents();
ActionListener slushac=new OkSlushac();
jButton1.addActionListener(slushac);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
kodot e izostaven
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame3().setVisible(true);
}
});
}
// Variables declaration - do not modify
promenlivite se izostaveni
// End of variables declaration
private class OkSlushac implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] niza;
niza=new String[10];
int i;
for (i = 1; i <= 10; i++) {
niza[i]="jTextField"+String.valueOf(i);
}
System.out.println("Inserting values in Mysql database table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "dental";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","admin");
try{
Statement st = con.createStatement();
String sql= new String();
String sql1=new String();
String sql2=new String();
String sql3=new String();
sql="insert into akaunti values {";
for (i = 1; i <= 10; i++) {
sql1+="'"+niza[i]+"',";
}
sql2="}";
sql3=sql+sql1+sql2;
int val = st.executeUpdate(sql3);
System.out.println("1 row affected");
}
catch (SQLException s) {
System.out.println("SQL statement is not executed!");
}
}
catch (SQLException n){
n.printStackTrace();
}
}
}
}
ИмашEve ja najnovata greska
NewJFrame3.java:179: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName(driver);
Eve ja najnovata verzija na kodot:
...
try{
Class.forName(driver);
.....
}catch(SQLException n){...}
[B]
Class.forName(driver);[/B]
}catch([B]Exception [/B]n){...}
}catch(SQLException n){
}catch(ClassNotFoundException cnfe){
}