A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

使用JDBC-ODBC 方式如何连接MySQL数据库

评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

1 个回复

正序浏览
  1. public static void main(String[] args) {

  2.                 Connection conn = null;
  3.                 Statement st = null;
  4.                
  5.                 try {
  6.                         // 1.注册驱动
  7.                         String driverName = "com.mysql.jdbc.Driver";

  8.                         Class.forName(driverName);
  9.                         // DriverManager.registerDriver(new Driver());

  10.                         // 2.创建连接
  11.                         String url = "jdbc:mysql://localhost:3306/test";
  12.                         String username = "root";
  13.                         String password = "passw0rd";

  14.                         conn = DriverManager.getConnection(url, username,
  15.                                         password);

  16.                         // 3.获得Statement
  17.                         st = conn.createStatement();

  18.                         // 4.执行SQL
  19.                         String sql = "insert into user (usernane, age) " +
  20.                                         "values ('cc', 22)";

  21.                         //String sql2 = "update user set username='zsdsdf' " +
  22.                         //                "where username='cc'";
  23.                        
  24.                         int n = st.executeUpdate(sql);
  25.                 }

  26.                 catch (Exception e)
  27.                 {
  28.                         e.printStackTrace();
  29.                 }
  30.                 finally
  31.                 {
  32.                        
  33.                         try
  34.                         {
  35.                                 // 6.关闭资源
  36.                                 st.close();
  37.                                 conn.close();
  38.                         }
  39.                         catch (SQLException e)
  40.                         {
  41.                                 e.printStackTrace();
  42.                         }
  43.                        
  44.                 }
  45.         }
复制代码
以前的代码  供你参考。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马