Re: JDBC connect to dabase fails, OK using ODBC
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Fri, 31 Aug 2007 10:03:09 -0400
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
.
- References:
- Re: JDBC connect to dabase fails, OK using ODBC
- From: dilip
- Re: JDBC connect to dabase fails, OK using ODBC
- Prev by Date: Re: JDBC connect to dabase fails, OK using ODBC
- Previous by thread: Re: JDBC connect to dabase fails, OK using ODBC
- Next by thread: Examples of real usage of outer joins?
- Index(es):