1.jdbc 连接mysql时中的URL怎么写的
String url="jdbc:mysql://localhost:3306/sample_db?user=root&password=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=root&password=root&useUnicode=true&characterEncoding=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(); } } }。
2.连接mysql的url写法
这样:
jdbc:mysql://<hostname>[<:3306>]/<dbname>
jdbc:mysql://localhost:3306/db_librarySys
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/db_librarySys?user=root&password=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;数据库集群时候常用此连接,比上面那个多点功能,性能好点。
3.连接mysql的url写法
这样:jdbc:mysql://
1、协议名总是“jdbc”。 2、子协议名由JDBC驱动程序的编写者决定。
3、数据源名也可能包含用户与口令等信息;这些信息也可单独提供。URL:jdbc:oracle:thin:@machine_name:port:dbname 注:machine_name:数据库所在的机器的名称; port:端口号,默认是1521Class.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;数据库集群时候常用此连接,比上面那个多点功能,性能好点。
4.java链接mysql数据库url怎么写
连接代码如下:
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( e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
5.eclipse用jdbc连接mysql数据库时,url是填什么
1、首先登陆mysql,查看mysql的数据情况,select * from test_data1 t
2、新建java类,测试jdbc功能
3、编写java的jdbc代码,
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8&useSSL=false";
String user = "root";
String pwd = "123456";
4、代码中查询mysql数据表,并执行查出表中内容;select * from test_data1
6.java链接mysql数据库url怎么写
连接代码如下: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( e) {System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace();} catch(SQLException e) {e.printStackTrace();} catch(Exception e) {e.printStackTrace();}。
7.mybatis连mysql的url怎么写
mybatis连mysql的url怎么写
最近公司项目要使用myBatis,自己以前没有接触过,就在网上找到了一些资料研究了些。初步做出了基于myBatis连接mysql数据库的jdbc实现的功能。
employee.java
package com.org.position.model;
public class employee {
private int employeeId;// 员工id
private String employeeName; //员工姓名
private String employeeSax; //员工性别
private String employeePost; //员工职务
private String employeeDepartment; //员工所在部门
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeSax() {
return employeeSax;
}
public void setEmployeeSax(String employeeSax) {
this.employeeSax = employeeSax;
}
public String getEmployeePost() {
return employeePost;
}
public void setEmployeePost(String employeePost) {
this.employeePost = employeePost;
}
public String getEmployeeDepartment() {
return employeeDepartment;
}
public void setEmployeeDepartment(String employeeDepartment) {
this.employeeDepartment = employeeDepartment;
}
}
转载请注明出处育才学习网 » mysql连接数据库url怎么写