Results 1 to 4 of 4
Thread: sql connection with java
- 02-26-2011, 03:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
sql connection with java
hi.. im trying to connect sqlserver with java ..
im getting this error..
Exception caught=java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Dri
ver
can anyone pl clear this:
this is my connection codings:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:jtds:sqlser ver://localhost:1433/veni","","");
System.out.println("connected");
- 02-26-2011, 10:08 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
hm...
I made a DB with microsoft accsses. and I do it like that:
That's with Accsses. If you use any other database you should google it for it.Java Code:import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.*; public class conection { private static final String USERNAME = "admin"; private static final String PASSWORD = "welcome"; private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver"; private static final String URL ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=test.mdb;}"; //make an URL to my database test.mdb which must be in your project folder. public static void main(String[]args)throws Exception{ Connection conn=null; try {//lot can go wrong here. Class.forName(DRIVER); conn= DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("WRITE SQL HERE"); while ( rs.next() ) { String l = rs.getString("Id");// your sql record saved as string System.out.println(l);//writes your sql record } conn.close(); } catch (SQLException e) { e.printStackTrace(); } }}
- 03-06-2011, 09:06 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Firstly make sure that mysql-connector-java-5.0.8-bin.jar is in
C:\Program Files\Java\jdk1.6.0_01\jre\lib\ext
then edit your code here: I have just pasting a sample
Java Code:String database="shop",table,user="root",password="123"; Connection m_Connection = null; Statement m_Statement = null,mst=null; ResultSet m_ResultSet = null; String m_Driver ="com.mysql.jdbc.Driver"; String m_Url = "jdbc:mysql://localhost:3306/shop"; try { Class.forName(m_Driver); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } String query =""; try { //Create connection object m_Connection = DriverManager.getConnection(m_Url, user, password); //Create Statement object m_Statement = m_Connection.createStatement(); query="SELECT *FROM profile"; //Execute the query double vat=0,discount=0; m_ResultSet=m_Statement.executeQuery(query); if(m_ResultSet.next()){ statement } //////////// ////////////////////////// m_ResultSet.close(); m_Connection.close(); } catch (SQLException ex) { ex.printStackTrace(); System.out.println(query); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
- 03-07-2011, 10:55 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Wrong in so many ways.
The OP is using the jtds driver for SQLServer.
So a MySQL driver will be no use.
All they need to do is ensure the jar file is on the runtime classpath.
Which leads to the second big mistake. Do not stick stuff under the JDK directory structure! You should have a proper runtime classpath defined for your app (either -cp for the java command line command, or a Class-Path entry in you apps jar file).
Similar Threads
-
java connection to mssql
By gerard kowara in forum JDBCReplies: 9Last Post: 10-14-2010, 09:07 AM -
Java.net.socket connection :connection closed
By veeru541 in forum Advanced JavaReplies: 2Last Post: 06-27-2010, 02:14 AM -
Connection In Java
By SaraHrry in forum Advanced JavaReplies: 7Last Post: 01-08-2010, 05:47 PM -
java mysql connection
By sysout in forum New To JavaReplies: 5Last Post: 10-31-2009, 10:48 AM -
we implement connection pooling ourselves, but why it always out of connection ?
By zengqingyi12 in forum JDBCReplies: 7Last Post: 10-20-2009, 10:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks