jdbc to my sql connectivity
This is my java code for jdbc connectivity:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class dbtest
{
public static void main(String args[])throws ClassNotFoundException,SQLException
{
String host = "127.0.0.1.mysql.com";
String dbName = "emp";
int port = 3306;
String URL = "jdbc:mysql://127.0.0.1/emp";
try
{
Class.forName("jdbc:mysql://127.0.0.1/emp");
}
catch (ClassNotFoundException cnfe) {
System.out.println("Error loading driver: ");
}
String password = "pavilion";
Connection connection =DriverManager.getConnection(URL,username,password);
DatabaseMetaData dbMetaData =connection.getMetaData();
String productName =dbMetaData.getDatabaseProductName();
System.out.println("Database: " + productName);
productVersion =dbMetaData.getDatabaseProductVersion();
System.out.println("Version: " + productVersion);
Statement statement = connection.createStatement();
String query ="SELECT * FROM emp";
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next())
{
System.out.println(resultSet.getString(1) + " " +resultSet.getString(2));
}
connection.close();
}
}
Errors:
Error loading driver:
Exception in thread "main" java.sql.SQLException: No suitable driver found
forjdbc:mysql://127.0.0.1/emp
at java.sql.Dr开发者_JAVA百科iverManager.getConnection(DriverManager.java:640)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at dbtest.main(dbtest.java:40)
instead of this
Class.forName("jdbc:mysql://127.0.0.1/emp");
do this
Class.forName("com.mysql.jdbc.Driver");
精彩评论