如何用Java與mySQL連接,以及如何把Java程序打包。。。。。。。。
1.導入mysql的jar包
2.編寫代碼
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Jdbc {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://127.0.0.1:3306/test";//把test換成你自己的數據庫名字
private static final String user="root";//換成自己的用戶名
private static final String password="root";//換成自己的密碼
private static Connection conn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
private static String sql=null;
public static void main(String[] args) {
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
System.out.println(conn);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}