Jump to content

erro quando tento fazer a ligação a base de dados SQL Server


Recommended Posts

Posted (edited)

olá a todos... antes de mais peço desculpa se não coloquei o tópico no sitio correcto.

Bem o meu problema é o seguinte, quando tento fazer a ligação através do java a uma base em de dados (sql server) é dado o seguinte erro:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/database
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at DirectoryWatcher.dataBase(dataManipulate.java:38)
at DirectoryWatcher.printEvent(dataManipulate.java:153)
at DirectoryWatcher.run(dataManipulate.java:183)
at java.lang.Thread.run(Thread.java:724)

não sei como instalar o tal driver este é o código que estou a usar:

Connection con = null;
	 PreparedStatement pst = null;
	 try (



			 // Step 1: Allocate a database "Connection" object
			 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "pass");
	 // MySQL
		 Connection connc = DriverManager.getConnection("jdbc:odbc:ebookshopODBC"); // Access

			 // Step 2: Allocate a "Statement" object in the Connection
			 Statement stmt = conn.createStatement();
		 ) {
			 // Step 3: Execute a SQL SELECT query, the query result
			 // is returned in a "ResultSet" object.
			 String strSelect = "SELECT * from tbl";
			 System.out.println("The SQL query is: " + strSelect); // Echo For debugging
			 System.out.println();

			 ResultSet rset = stmt.executeQuery(strSelect);

			 // Step 4: Process the ResultSet by scrolling the cursor forward via next().
			 // For each row, retrieve the contents of the cells with getXxx(columnName).
			 System.out.println("The records selected are:");
			 int rowCount = 0;
			 while(rset.next()) { // Move the cursor to the next row
			 String title = rset.getString("title");
			 double price = rset.getDouble("price");
			 int qty = rset.getInt("qty");
			 System.out.println(title + ", " + price + ", " + qty);
			 ++rowCount;
			 }
			 System.out.println("Total number of records = " + rowCount);

		 } catch(SQLException ex) {
			 ex.printStackTrace();
		 }

agradecia muito se alguem me pudesse ajudar .....

Obrigado 🙂

Edited by Baderous
geshi
Posted

Boas desculpem só responder agora...

quando criei o post enganei-me a copiar o código peço as minhas desculpas por isso...mas já resolvi o meu problema que era quando eu usava uma Statement para todas as operações mas de qualquer das maneiras obrigados por me responderem...abraço a todos 😉

  • 2 weeks later...
Posted

Boas aquele cujo anda com a casa às costas,

Desconheço tal declaração JDBC: Connection connc = DriverManager.getConnection("jdbc:odbc:ebookshopODBC"); // Access

vê o 4º post desta thread http://www.portugal-a-programar.pt/topic/61714-conexao-base-de-dados/

isso é para MySQL se quiseres um exemplo de MSSQL diz

era só uma questão de pesquisares

http://en.wikipedia.org/wiki/JDBC_driver

http://www.roseindia.net/jdbc/Jdbc-odbc-connection.shtml

Podes usar este segundo link para o odbc

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.