JDBC操作数据库(SQL server)——创建数据库代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class Test {

    static Connection dbConn=null;
    public static void main(String[] args) {

        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL="jdbc:sqlserver://localhost:1433;";
        String userName="TangHao";
        String userPwd="tanghao6.1";
        try {
            Class.forName(driverName);
            dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
            System.out.println("数据库连接成功");
        } catch ( Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("连接失败");
        }

        Statement sta = null;

        //创建数据库的sql语句:
        String creDateBase = " create DATABASE 超级数据库 ON PRIMARY " +
                "(NAME='super_db',FILENAME= 'D:\\super.MDF'," +
                "SIZE=5MB,MAXSIZE=15MB,FILEGROWTH=10%)LOG ON" +
                "(NAME='super_LOG',FILENAME='D:\\super_LOG.LDF'," +
                "SIZE=1024KB,MAXSIZE=3MB,FILEGROWTH=128KB);";
        try {
            sta=dbConn.createStatement();
            int count=sta.executeUpdate(creDateBase);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

这是JDBC连接SQL server并创建一个数据库的代码。

过程中遇到过报错

译为:字符串文字中的非法转义字符。

解决:代码中出现字符‘\’时应用两个字符\\代替

猜你喜欢

转载自blog.csdn.net/QiuBika_061/article/details/84928688
今日推荐