package com.sample;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
public class connectionclass {
public static void main(String args) {
System.out.println("MySql Connect Example");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "testdatabase";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = (Connection) DriverManager.getConnection(url + dbName,
userName, password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
此代码连接到数据库,不会出现语法错误。我在Eclipse中这样做,当我运行项目时,它会询问我们需要运行哪个类,但是我的课不在那里。
答案 0 :(得分:3)
请更正此行
public static void main(String[] args)
答案 1 :(得分:1)
main方法接受String数组的参数 所以这样做
public static void main(String args[])