mysql怎么配置url mysql怎么配置环境

java链接mysql数据库url怎么写

连接代码如下:

创新互联公司是一家专业提供民权企业网站建设,专注与成都网站设计、成都网站建设H5响应式网站、小程序制作等业务。10年已为民权众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

public static void main(String[] args){

// 驱动程序名

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

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

String url = "jdbc:mysql://127.0.0.1:3306/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("-----------------");

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

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

String name = null;

while(rs.next()) {

// 选择sname这列数据

name = rs.getString("sname");

// 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。

// 然后使用GB2312字符集解码指定的字节数组

name = new String(name.getBytes("ISO-8859-1"),"GB2312");

// 输出结果

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

}

rs.close();

conn.close();

} catch(ClassNotFoundException e) {

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

e.printStackTrace();

} catch(SQLException e) {

e.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

}

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

String url="jdbc:mysql://localhost:3306/sample_db?user=rootpassword=your_password";

例如:

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数据库

String url = "jdbc:mysql://localhost:3306/javademo?"

+ "user=rootpassword=rootuseUnicode=truecharacterEncoding=UTF8";

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();

sql = "create table student(NO char(20),name varchar(20),primary key(NO))";

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

if (result != -1) {

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

sql = "insert into student(NO,name) values('2012001','陶伟基')";

result = stmt.executeUpdate(sql);

sql = "insert into student(NO,name) values('2012002','周小俊')";

result = stmt.executeUpdate(sql);

sql = "select * from student";

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

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

while (rs.next()) {

System.out

.println(rs.getString(1) + "\t" + rs.getString(2));// 入如果返回的是int类型可以用getInt()

}

}

} catch (SQLException e) {

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

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

conn.close();

}

}

}

连接mysql的url写法

这样:

jdbc:mysql://hostname[:3306]/dbname

jdbc:mysql://localhost:3306/db_librarySys

Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/db_librarySys?user=rootpassword=1234");

Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/db_librarySys", "root", "1234");

扩展资料:

注意事项

URL=协议名+子协议名+数据源名。

1、协议名总是“jdbc”。

2、子协议名由JDBC驱动程序的编写者决定。

3、数据源名也可能包含用户与口令等信息;这些信息也可单独提供。

URL:jdbc:oracle:thin:@machine_name:port:dbname

注:machine_name:数据库所在的机器的名称;

port:端口号,默认是1521

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

String url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl为数据库的SID

String user="test";

String password="test";

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

注意:Oracle的URL有两种写法:

1、jdbc:oracle:thin:@localhost:1521:databaseName   常用操作sql的工具:sqlDeveloper.exe,还可以用其他数据库,如mysql等

2、jdbc:oracle:oci:@localhost:1521:databaseName    用来操作SQL的工具只能用:PL/SQL Developer;数据库集群时候常用此连接,比上面那个多点功能,性能好点。


网站题目:mysql怎么配置url mysql怎么配置环境
转载源于:http://ybzwz.com/article/dogsjei.html