java - mysql connection issue in first try -
i have been learning java 3 weeks. wanted connect mysql java , i'm trying now. there problem. have installed php,apache,mysql 3 in ubuntu. , can reach phpmyadmin port 80
. think there problem when connecting database why said port.
here table:
+-------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-------+-------------+------+-----+---------+----------------+ | id | int(2) | no | pri | null | auto_increment | | name | varchar(32) | no | | null | | +-------+-------------+------+-----+---------+----------------+
and here java code:
import java.sql.drivermanager; import java.sql.statement; public class main { public static void main(string args[]) { try { java.sql.connection conn = drivermanager.getconnection("jdbc:mysql://localhost:80/test", "root", "1"); statement state = (statement) conn.createstatement(); string name = "try"; state.executeupdate("insert deneme (name) values (" + name + ")"); } catch (exception e) { } } }
this code runs 20 seconds , returns build successful (total time: 21 seconds)
idea problem?
the single quotes fix problem...
state.executeupdate("insert deneme (name) values ('" + name + "')");
by using preparedstament not need mess single , double quote combinations.
note reason why try
statement required, in order report exception, since many things may go wrong when accessing database report exception message , find problem.
also, useful check if jdbc driver exists before trying connect. insert try statement, one:
try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { string msg = "the com.mysql.jdbc.driver missing\n" + "install , rerun application"; joptionpane.showmessagedialog(this, msg, this.gettitle(), joptionpane.error_message); system.exit(1); }
Comments
Post a Comment