网站首页 > 文章中心 > 其它

mysql的url怎么写_mysql url怎么写

作者:小编 更新时间:2023-08-28 14:44:53 浏览量:292人看过

jdbc 连接mysql时中的URL怎么写的

例如:

import?java.sql.DriverManager;

import?java.sql.ResultSet;

import?java.sql.SQLException;

import?java.sql.Connection;

import?java.sql.Statement;

public?class?MysqlDemo?{

public?static?void?main(String[]?args)?throws?Exception?{

Connection?conn?=?null;

String?sql;

//?MySQL的JDBC?URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值

//?避免中文乱码要指定useUnicode和characterEncoding

//?执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,

//?下面语句之前就要先创建javademo数据库

try?{

//?之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,

//?可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以

Class.forName("com.mysql.jdbc.Driver");//?动态加载mysql驱动

//?or:

//?com.mysql.jdbc.Driver?driver?=?new?com.mysql.jdbc.Driver();

//?or:

//?new?com.mysql.jdbc.Driver();

System.out.println("成功加载MySQL驱动程序");

//?一个Connection代表一个数据库连接

conn?=?DriverManager.getConnection(url);

//?Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等

Statement?stmt?=?conn.createStatement();

int?result?=?stmt.executeUpdate(sql);//?executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功

if?(result?!=?-1)?{

System.out.println("创建数据表成功");

result?=?stmt.executeUpdate(sql);

sql?=?"select?*?from?student";

ResultSet?rs?=?stmt.executeQuery(sql);//?executeQuery会返回结果的集合,否则返回空值

System.out.println("学号\t姓名");

while?(rs.next())?{

System.out

}

}?catch?(SQLException?e)?{

System.out.println("MySQL操作错误");

e.printStackTrace();

}?catch?(Exception?e)?{

}?finally?{

conn.close();

java链接mysql数据库url怎么写

连接代码如下:

public static void main(String[] args){

// 驱动程序名

String driver = "com.mysql.jdbc.Driver";

// URL指向要访问的数据库名scutcs

// MySQL配置时的用户名

String user = "root";

// MySQL配置时的密码

String password = "root";

try {

// 加载驱动程序

Class.forName(driver);

// 连续数据库

Connection conn = DriverManager.getConnection(url, user, password);

if(!conn.isClosed())

System.out.println("Succeeded connecting to the Database!");

// statement用来执行SQL语句

Statement statement = conn.createStatement();

// 要执行的SQL语句

String sql = "select * from student";

// 结果集

ResultSet rs = statement.executeQuery(sql);

System.out.println("-----------------");

System.out.println("执行结果如下所示:");

System.out.println(" 学号" + "\t" + " 姓名");

String name = null;

while(rs.next()) {

// 选择sname这列数据

name = rs.getString("sname");

// 输出结果

System.out.println(rs.getString("sno") + "\t" + name);

rs.close();

} catch(ClassNotFoundException e) {

System.out.println("Sorry,can+t find the Driver!");

} catch(SQLException e) {

} catch(Exception e) {

mysql的jdbcurl怎么写

jdbc:mysql:// 是指JDBC连接方式;

localhost: 是指你的本机地址;

test 就是你要连接的数据库的地址.

以上就是土嘎嘎小编为大家整理的mysql的url怎么写相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章