Results 1 to 4 of 4
  1. #1
    veni is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default 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");

  2. #2
    kekcklemen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Wink

    hm...
    I made a DB with microsoft accsses. and I do it like that:

    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();
    } 
    }}
    That's with Accsses. If you use any other database you should google it for it.

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    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());
    
        }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    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
    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

  1. java connection to mssql
    By gerard kowara in forum JDBC
    Replies: 9
    Last Post: 10-14-2010, 09:07 AM
  2. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  3. Connection In Java
    By SaraHrry in forum Advanced Java
    Replies: 7
    Last Post: 01-08-2010, 05:47 PM
  4. java mysql connection
    By sysout in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 10:48 AM
  5. Replies: 7
    Last Post: 10-20-2009, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •