我得到了以下错误:
我尝试了各种方法,我得到了一个错误,请看下面的错误。
运行: java.net.URLClassLoader.findClass(URLClassLoader.java:381),java.lang.ClassLoader.loadClass(ClassLoader.java:424),sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338),java.lang.ClassLoader.loadClass(ClassLoader.java:357),java.lang.Class.forName0(原生方法),java.lang.Class.forName(Class.java )的java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver:264)在com.sqldbsamples.App.main(App.java:23)
成功构建(总时间:0秒)
这是我的代码:请帮助我如何连接我的数据库,使用java程序!
public class App {
public static void main(String[] args) {
// Connect to database
String hostName = "testchinnaa.database.windows.net:1433"; // update me
String dbName = "Test_Gopi"; // update me
String user = "chinna"; // update me
String password = "******"; // update me
String url = String.format("jdbc:sqlserver://testchinnaa.database.windows.net:1433;database=Test_Gopi;user=chinna@testchinna;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
+ "hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
Connection connection = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(url);
String schema = connection.getSchema();
System.out.println("Successful connection - Schema: " + schema);
System.out.println("Query data example:");
System.out.println("=========================================");
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName "
+ "FROM [SalesLT].[ProductCategory] pc "
+ "JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
// Print results from select statement
System.out.println("Top 20 categories:");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + " "
+ resultSet.getString(2));
}
connection.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}发布于 2020-02-25 20:13:36
对不起,您必须弄清楚您是在使用MySql还是MSSql。你说你用了mysql。但是,在连接字符串中,它是一个sqlserver,这意味着它是一个MSSql。
下面是使用java访问数据库的教程:
- For MySql: [MySQL Connector/J](https://mvnrepository.com/artifact/mysql/mysql-connector-java)
- For MSSql: [Microsoft JDBC Driver for SQL Server](https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc)
中安装和配置连接器/J库。
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();连接= DriverManager.getConnection("conntection_string");字符串SQL =“从系统数据库中选择名称”;
- For MSSql:(语句= connection.createStatement();ResultSet resultSet =statement.executeQuery(SQL){ // Print结果来自select语句,而(resultSet.next()) { System.out.println(resultSet.getString(1));} connection.close();} catch (异常e) {e.printStackTrace()};
-关于MySql:
Connection conn = null;ResultSet rs = null;try { Class.forName("com.mysql.cj.jdbc.Driver");String connectionUrl =connectionUrl= DriverManager.getConnection(connectionUrl,"username_from_portal,例如:jack@mysqldemo258 258“,"password");rs =conn.prepareStatement(”显示数据库“).executeQuery();而(rs.next()){ System.out.println(rs.getString(1));} catch (ClassNotFoundException e) { e.printStackTrace();} catch (SQLException e) { e.printStackTrace();}最后{ try{ if(rs = null) rs.close();if(conn != null) conn.close();} catch (SQLException e) { e.printStackTrace();}}
https://stackoverflow.com/questions/60395665
复制相似问题