•创建一个以JDBC连接数据库的程序,包含7个步骤:
1、加载JDBC驱动程序:
在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),
这通过java.lang.Class类的静态方法forName(String className)实现。
例如:
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace() ;
}
成功加载后,会将Driver类的实例注册到DriverManager类中。
2、提供JDBC连接的URL
•连接URL定义了连接数据库时的协议、子协议、数据源标识。
•书写形式:协议:子协议:数据源标识
协议:在JDBC中总是以jdbc开始
子协议:是桥连接的驱动程序或是数据库管理系统名称。
数据源标识:标记找到数据库来源的地址与连接端口。
例如:(MySql的连接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字符集。如果characterEncoding设置为
gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式。
3、创建数据库的连接
•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象,
该对象就代表一个数据库的连接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法传入指定的欲连接的数据库的路径、数据库的用户名
Ⅱ jdbc怎么连接mysql数据库
首先导入驱动jar包,程序是
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("数据库连接成功,连接id是:"+con);
}
}
}
Ⅲ 怎样用jdbc连接mysql数据库
importjava.sql.*;
publicclassMysqlTest{
publicstaticvoidmain(String[]args){
//驱动程序名
Stringdriver="com.mysql.jdbc.Driver";
//URL指向要访问的数据库名world
Stringurl="jdbc:mysql://127.0.0.1:3306/world";
//MySQL配置时的用户名
Stringuser="root";
//MySQL配置时的密码
Stringpassword="123456";
Stringname;
try{
//加载驱动程序
Class.forName(driver);
//连续数据库
Connectionconn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
System.out.println("!");
//statement用来执行SQL语句
Statementstatement=conn.createStatement();
//要执行的SQL语句
Stringsql="select*fromcity";
//结果集
ResultSetrs=statement.executeQuery(sql);
while(rs.next()){
//选择Name这列数据
name=rs.getString("Name");
//输出结果
System.out.println(rs.getString("CountryCode")+" "+name);
}
rs.close();conn.close();}
catch(ClassNotFoundExceptione){
System.out.println("Sorry,can`tfindtheDriver!");
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
}
Ⅳ 如何使用jdbc连接mysql数据库
StringdriverName="com.mysql.jdbc.Driver";//加载JDBC驱动
StringdbURL="jdbc:mysql://localhost:3306/test";//连接服务器和数据库test
StringuserName="sa";//默认用户名
StringuserPwd="sa";//密码
java.sql.ConnectiondbConn;
try{
Class.forName(driverName).newInstance();
dbConn=java.sql.DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("ConnectionSuccessful!");//如果连接成功控制台输出ConnectionSuccessful!
}catch(ClassNotFoundExceptione){
System.out.println("没有找到驱动");
}catch(Exceptione){
//TODO:handleexception
e.printStackTrace();
}
还是直接上代码吧。
Ⅳ jdbc怎么连接mysql数据库
String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动String dbURL = "jdbc:mysql://localhost:3306/test"; // 连接服务器和数据库testString userName = "sa"; // 默认用户名String userPwd = "sa"; // 密码java.sql.Connection dbConn; try { Class.forName(driverName).newInstance(); dbConn = java.sql.DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!} catch (ClassNotFoundException e) { System.out.println("没有找到驱动");} catch (Exception e) { // TODO: handle exception e.printStackTrace();}
Ⅵ 如何使用jdbc连接mysql数据库
用Jdbc连接MySql服务器还是很方便的。
首先,将jdbc导入工程,或者将jdbc放到ClassPath里,这里我利用Eclipse直接导入jdbc jar文件,不罗嗦了。
然后,制定DriverManager,利用最简单的方法,Class类的froName直接完成,代码:
Class.forName("com.mysql.jdbc.Driver").newInstance();
然后,实例化一个链接Connection,注意用户名和密码,有几个方法可供选择,这里我用的是DirverManager类的getConnection(String url, String user, String password)方法。具体使用:DriverManager
例如:Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", "1234");
下一步,建立用于执行sql语句的Statement,这个容易,一行代码搞定:
Statement stat=conn.createStatement();
最后就可以利用stat实例执行sql语句了
Ⅶ 如何实现JDBC连接mysql数据库
1.导入mysql数据库的驱动jar包
如我的导的是mysql-connector-java-5.1.40.jar
2.注册驱动
注册驱动代码为:Class.forName("com.mysql.jdbc.Driver")
3.获取连接
Connection con = DriverManager.getConnection(url,username,password)
其中url为 jdbc:mysql://localhost:3306/xun?useSSL=false,后面那个xun为数据库的库名,如果不使用加密的话,?useSSL=false这个其实也可以省略,省略后为jdbc:mysql://localhost:3306/xun,不过最好还是别省略哈。
4.获取PrepareStatement并发送sql查询语句
String sql = "select * from user where username = ?"(这个查询语句为随便举的例子哈)
PreparedStatement pst = con.preparedStatement(sql);
pst.setString(1,"zengjiaxun");
ResultSet rs = pst.executeQuery();
注意,执行查询使用的是executeQuery()方法,此方法返回的是ResultSet,ResultSet为结果集,封装了查询结果。插入,删除,修改用的是executeUpdate()方法。
pst.clearParameters();
执行sql后记得关闭相应的流。。
Ⅷ jdbc怎么连接mysql数据库
首先导入驱动jar包,程序是
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("数据库连接成功,连接id是:"+con);
}
}
}
Ⅸ 使用jdbc连接mysql为什么报错
当我用JDBC连接MySql数据库时,编译报了如下错误:
错误1:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
这要求我们注册驱动时,把Class.forName("com.mysql.jdbc.Driver");改成 Class.forName("com.mysql.cj.jdbc.Driver");
当我信息满满的修改之后重新编译时,再次出现了错误:
错误2:
Fri Feb 22 08:55:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
这要求我们在设置url参数时,将useSSL=false,修改后 jdbc:mysql://localhost:3306/ds3?useSSL=false
当我修改后,本以为这下应该没问题了,没想到,再一次出现了问题
错误3:
Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
这要求我们修改时区,修改成jdbc:mysql://localhost:3306/ds3?useSSL=false&serverTimezone=UTC
终于,不在报错误了。
错误4:当我们配置xml文件时,要把&转为其本身的转义字符
配置properties文件的urlurl=jdbc:mysql:///ds3?useSSL=false&serverTimezone=UTC配置xml文件的url<property name="url">jdbc:mysql://localhost:3306/ds3?useSSL=false&serverTimezone=UTC</property>