How do i connect an SQL file to my Bluej application?
I really am new at this, and I don't really know what I'm doing so please be gentle. I have a blueJ program and an SQL database to connect it to. I have a .jar library to connect it but where exactly do I save the library and where exactly is that location stored? here, right?
try
{
String user = "root";
String pass = "12345";
String url = "jdbc:mysql://localhost/mydb";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, u开发者_StackOverflow社区ser, pass);
System.out.println ("Database connection established");
}
But in
String url = "jdbc:mysql://localhost/mydb";
The location is different from where I currently have my sql file and jar library stored, so I need to change this line to the location of the file and library? Is that right?
To be a numskull here, if I just saved it to the C drive (for the sake of easy understanding) how would I rewrite that line to point there?
Thanks. Whew.
import java.sql.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = 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();
}
}
}
精彩评论