- 论坛徽章:
- 0
|
java连接Mysql
Java代码- 1.import java.sql.Connection;
- 2.import java.sql.ResultSet;
- 3.import java.sql.Statement;
- 4.import java.util.ArrayList;
- 5.import java.util.Date;
- 6.import java.util.List;
- 7.
- 8.import com.ceo.dwr.DwrCity;
- 9.
- 10.import jxl.write.DateTime;
- 11.import net.sf.json.JSONArray;
- 12.
- 13.
- 14.//数据库连接管理
- 15.public class DataBaseManager {
- 16.
- 17. private final String driver="com.mysql.jdbc.Driver";
- 18. private final String user="root";
- 19. private final String pwd="wen";
- 20. public static Connection getMysqlCon(){
- 21. //myDB为数据库名
- 22. Connection conn=null;
- 23. try {
- 24. Class.forName("com.mysql.jdbc.Driver");
- 25. String url ="jdbc:mysql://localhost:3306/sns?useUnicode=true&characterEncoding=utf-8";
- 26.
- 27. conn= java.sql.DriverManager.getConnection(url, "root", "wen");
- 28.
- 29. } catch (ClassNotFoundException e) {
- 30. // TODO: handle exception
- 31. e.printStackTrace();
- 32.
- 33. }catch (Exception e) {
- 34. e.printStackTrace();
- 35. }
- 36. return conn;
- 37.
- 38. }
- 39.
- 40.
- 41.
- 42.}
复制代码 |
|