为了使事情变得更简单,您可以使用NetBeans 6.5,它使设置SQL数据库变得更加容易。我现在正在使用它们,它是GUI布局和数据库连接的救命稻草。下面是一些关于如何从NetBeans连接到MySQL数据库的代码:
//these are variables i declare in the beginning of my code
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/jtschema";
private Connection connection = null;
public static Statement statement = null;
public void initSQLServer() {
try {
Class.forName(DRIVER).newInstance();
try {
connection = DriverManager.getConnection(DATABASE_URL, "root", "Dropatrain!248");
statement = connection.createStatement();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
} catch (Exception ex) {
System.out.println(ex);
}
}