用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

数据库连接

2017-12-23 作者: 柯侧耳倾听者举报

[java]代码库

package mysql;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
public class TestConnect {
 
    public static void main(String[] args) {
         Connection con = null;
         
        //1.加载数据库驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("加载失败!");
            e.printStackTrace();
        }
        System.out.println("加载成功!");
     
        //2.连接数据库
        try {
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_library", "root", "123");
        } catch (SQLException e) {
            System.out.println("连接数据库失败!");
            e.printStackTrace();
        }
          System.out.println("成功连接数据库!");
           
         //3.执行sql语句语句的statement,然后执行sql语句
           
          /*sql创建表语句
           *create table table1(id int not null,
           *name varchar(20) not null,
           *age int not null,
           *primary key(id));
           */
           
          try {
            Statement statement = con.createStatement();
              int i=statement.executeUpdate("create table table1(id int not null auto_increment,name varchar(20) not null,age int not null,primary key(id))");
              //int i=statement.executeUpdate("drop table table1"); 
            System.out.println(i);
              System.out.println("建表成功!");
          } catch (SQLException e) {
              System.out.println("执行sql语句出错!");
            e.printStackTrace();
            return;
        }
          System.out.println("执行成功!");
                 
    }
 
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...