Witam
Ucze się łączyć program z bazą MySQL. Na sam start chce wykonać banalne operacje w mainie - zeby sobie nie zagmatwać.
Napisałem cos takiego:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class mainik {
public static void main(String[] args)
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","haslo");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE IF NOT EXIST maciektable (imie varchar(255),nazwisko varchar(255),pesel varchar(255))");
PreparedStatement prepStat = conn.prepareStatement("insert into maciektable values'(?,?,?)';");
prepStat.setString(1, "Maciek");
prepStat.setString(2, "Nazwisko");
prepStat.setString(3, "123123");
prepStat.execute();
conn.close();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Pobrałem baze danych ze strony mysqla: http://dev.mysql.com/downloads/mysql/ mam wersje MySQL 5.6
I connectora najnowszego, również ze strony mysql, podlaczylem go do projektu jako external jar.
I pokazuje mi sie błąd:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'EXIST maciektable(imie varchar(255),nazwisko varchar(255),pesel varchar(255))' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2761)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
at test.mainik.main(mainik.java:19)
Szukam juz 2gi dzień w internecie i nic nie daje efektu
moze wy wiecie ocb?:)
Przy okazji:
jakbym tworzył jakiś servlet i chcial polaczyc go z baza danych to w ten sam sposob?:)