澳门新萄京官方网站-www.8455.com-澳门新萄京赌场网址

澳门新萄京官方网站:JDBC链接数据库步骤,7简单

2019-11-24 作者:数据库网络   |   浏览(56)

java中定义链接数据库的专门的学业:JDBC

Oracel

1,希图jdbc的连接jar包,然后在eclipse中成立多少个java项目(不是web项目)。

生机勃勃,首先在MYSQL建设构造一个数据库,举个例子Geek99DB:

1、导包:差别数据库有不一致的jdbc驱动包,何况jdbc驱动包和数据库版本必得呼应

ResultSet rs = ps.executeQuery(); 

2,将jdbc的jar包build path 也许copy到java项目中都能够,等下交给项目结构图。

    create database Geek99DB;

2、测试

while(rs.next()){

3,给出 jdbcc类的连接代码,

    use Geek99DB;

3、写代码

rs.getInt(1)

package jdbcDemo;

    然后组建多个表CustomerTab:

 

rs.getString(4)

import java.sql.Connection;

    create table CustomerTab(id int primary key auto_increment,name varcahr(20),email varchar(20));

try {

}

import java.sql.DriverManager;

    显示表:

1、//加载JDBC驱动    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

// 从数据库中取验证

Connection ct = null;

ResultSet rs = null;

PreparedStatement ps = null;

try {

// 1加载驱动

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

// 2获得一而再再而三

ct = DriverManager.getConnection(

"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");

// 3创建PrepareStatement

ps = ct.prepareStatement("select * from users where id=? and passwd=?");

// 给?赋值

ps.setObject(1, id);

ps.setObject(2, pwd);

// 4实行操作

rs = ps.executeQuery();

// 5依照结果处理

if (rs.next()) {

// 表达该客户合法

request.getRequestDispatcher("/MainFrame").forward(request,

          response);

} else {

          request.setAttribute("errno", "客商名密码错误");

          request.getRequestDispatcher("/LoginServlet").forward(request,

response);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

// 关闭能源

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

rs = null;

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ps = null;

}

if (ct != null) {

try {

ct.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ct = null;

}

}

import java.sql.SQLException;

    show tables;

2、//创制连接

MySQL

import com.mysql.jdbc.ResultSet;

    添美金素:

Connection con=DriverManager.getConnection(

Class.forName("com.mysql.jdbc.Driver");

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

Statement state = conn.createStatement();

String sql="select password from usr where name='" usr "'";

String sql2="update usr set isLogin=1 where name='" usr "'";

ResultSet res = state.executeQuery(sql);

while(res.next()) {

String tmp = res.getString("password");

if(tmp.equals(passwd)) {

System.out.println(usr "登录成功");

int count = state.executeUpdate(sql2);

System.out.println("更新了" count "条数据");

res.close();

state.close();

conn.close();

return true;

}

}

res.close();

state.close();

conn.close();

return false;

import com.mysql.jdbc.Statement;

    insert into CustomerTbl(name,email)values('tam','123@qq.com');

"jdbc:sqlserver://localhost:1433; DataBaseName=TestDB","sa","123");

public class Jdbcc {

    呈现成分:

3、//创立查询语句

public static void main(String[] args) {

    select *from CustomerTbl;

String sql="select * from People where username='" name "'";

// TODO Auto-generated method stub

二,在ecplise下达成对数据库的得到:

4、//创建Statement对象

try {

第大器晚成,建设构造三个java项目,新建一个文本夹lab,将mysql-connector-java-5.1.22-bin.jar(JDBC的驱动程序,复制到此文件夹下,并增多到build path卡塔 尔(英语:State of Qatar);

Statement sta=con.createStatement();

Class.forName("com.mysql.jdbc.Driver");

下一场新建一个class文件,代码如下:

5、//重返结果集

//创造三个总是

package jdbctest;

ResultSet count=sta.executeQuery(sql);

Connection conn=

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

if(count.next()){

DriverManager.getConnection("jdbc:mysql://localhost:3306/first_test","root","123");

public class test01 {

    out.println("<script type='text/javaScript'>alert('该顾客已存在 ')</script>");

//使用DriverManager的getConnectin(String url , String username ,

 public static void main(String[] args) {
  // TODO Auto-generated method stub

 //转载至登入分界面  request.getRequestDispatcher("Exercise01/Index.jsp").forward(request,response);

//String password )方法传入内定的欲连接的数据库的不二诀要、数据库的顾客名和

//加载JDBC驱动
  Connection conn=null;
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();  
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

//密码来获得。

//连接到一定的数据库对象
  try {
   澳门新萄京官方网站:JDBC链接数据库步骤,7简单例子。conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Geek99DB","root","yuan");
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

//成立二个sql 语句援助对象。

//建立SQL语句对象,试行查询
  String sql="select id,name,email from CustomerTbl";
  try {
   澳门新萄京官方网站:JDBC链接数据库步骤,7简单例子。Statement stmt =conn.createStatement();
   ResultSet rs=stmt.executeQuery(sql);

Statement stm = (Statement) conn.createStatement();

//管理结果集
   while(rs.next()){
    int id=rs.getInt(1);
    String name=rs.getString(2);
    String email=rs.getString(3);
    System.out.println(id "," name "," email);
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();

//施行查询语句

//关闭数据连接
澳门新萄京官方网站,   if(conn!=null){
    try {
     conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

ResultSet  rs=(ResultSet)stm.executeQuery("select * from score");

 }

//输出结果

}

while(rs.next())

  }finally {澳门新萄京官方网站 1澳门新萄京官方网站 2

{

 

System.out.print(rs.getInt("id") ":" rs.getString("name") ":" rs.getString("address"));

}

//关闭连接

rs.close();

stm.close();

conn.close();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

4,结构图,通过yog可以查阅大家的first_test数据库和它下边的表数据协会,然后在eclipse中可以预知实施轻松询问语句,查询出多少。

澳门新萄京官方网站 3

5, 若是现身意气风发警示验证 :

Thu Jul 27 09:12:34 CST 2017 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.

不畏使用JDBC跟你的数据库连接的时候,你的JDBC版本与MySQL版本分裂盟,MySQL的本子更加高级中学一年级些,在三翻五次语句后拉长“useSSL=‘true’” ,就能够连绵不断到数据库了。更加高版本.

如下

?characterEncoding=utf8&useSSL=true

=

=

8-31号更新,在servlet中三番四次数据库

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

@WebServlet("/CheckServlet")

public class CheckServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public  static final String  DEVICE="com.mysql.jdbc.Driver";

public static  final String URL="jdbc:mysql://localhost:3306/first_test?characterEncoding=utf8&useSSL=true";

public static final String USERNAME="root";

public static final String PASSWORD="123456";

public CheckServlet() {

super();

// TODO Auto-generated constructor stub

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.getWriter().append("Served at: ").append(request.getContextPath());

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html");

Connection conn =null;

PreparedStatement stm = null;

ResultSet resultSet= null;

PrintWriter  out = response.getWriter();

String userid=request.getParameter("userid"); //选取验证userid

try {

Class.forName(DEVICE);

//创造三个接连

conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);

//使用DriverManager的getConnectin(String url , String username ,

//String password )方法传入钦赐的欲连接的数据库的门路、数据库的顾客名和//密码来获得。

//创制一个sql语句支持对象。

String sql="SELECT COUNT(userid) FROM new_user WHERE userid=?";

stm = (PreparedStatement) conn.prepareStatement(sql);  //实例化 stm

stm.setString(1, userid); //设置查询参数

//试行查询操作

resultSet = stm.executeQuery();

if (resultSet.next()) {

if (resultSet.getInt(1)>0) {

out.println("true");  //输出音讯

}else {

out.println("false");

}

}

//关闭连接

resultSet.close();

stm.close();

conn.close();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

本文由澳门新萄京官方网站发布于数据库网络,转载请注明出处:澳门新萄京官方网站:JDBC链接数据库步骤,7简单

关键词: