Re: JDBC connect to dabase fails, OK using ODBC



dilip wrote:
On Jul 31, 2:02 pm, kma...@xxxxxxxxxxxxxxx wrote:
Hi friends,
myself dilip here are the following code for JDBC Connectivity.
Use that sample code according to your database name.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:<DSN_NAME>","User_Name","Password");
Statement st=con.createStatement();

above code may solve your difficulty hopefully.

Just to add to dilip's good advice, notice that he's given you just the basic method calls. You have to add exception handling (forName() can throw various exceptions such as ClassNotFoundException, createStatement() can throw SQLException, ...). You need a finally{} block after your access code to close the connection. In other words, dilip gave you a signpost, not a road.

Oh, and prefer prepareStatement() to createStatement(). The latter has security vulnerabilities (google for "SQL injection attack"), and most database engines (I'm not sure about MySQL) can optimize the former.

--
Lew
.