码迷,mamicode.com
首页 > 数据库 > 详细

Java connect to MySQL through JDBC

时间:2015-08-03 20:39:09      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

  1. Download the zip file 技术分享 from http://dev.mysql.com/downloads/connector/j/5.0.html

And then Extract to "mysql-connector-java-5.0.8" file.

  1. Open the Eclipse, Click "Project -> Properties->Java Build Path", click "Add External JARS"

技术分享

  1. Select the Extract file "mysql-connector-java-5.0.8-bin.jar"

技术分享

  1. When add the driver connector, it will not occur the ClassNotFoundException

    ?

  2. Create Database

create database schoolmanage;

use schoolmanage;

?

create table teacherinfo

(

name char(8) not null,

code char(8) not null primary key,

sex char(8) not null,

age char(8) not null,

birthday char(8) not null,

address char(8) not null,

salary char(8) not null

)

?

insert into teacherinfo(name,code,sex,age,birthday,address,salary) values ("Mr wang","2009","woman","1949-3","Beijing","7888");

insert into teacherinfo(name,code,sex,age,birthday,address,salary) values ("Mr LiU","2002","man","1989-5","ChongQ","4548");

insert into teacherinfo(name,code,sex,age,birthday,address,salary) values ("Mr LI","2005","man","1909-3","ShangH","7855");

insert into teacherinfo(name,code,sex,age,birthday,address,salary) values ("Mr Zhou","2005","woman","1999-3","Tianjin","677");

insert into teacherinfo(name,code,sex,age,birthday,address,salary) values ("Mr Xu","2021","woman","1989-3","ChongQ","34288");

?

  1. Java Program

package com.mysql;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

?

class JDBC_SQL {

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

{

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

String url = "jdbc:mysql://127.0.0.1:3306/schoolmanage";

String user = "root";

String password = "829915";

?

try

{

Class.forName(driver); //加载JDBC驱动

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

?

if(!con.isClosed())

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

Statement st = con.createStatement();

?

String m_name = "2001";

String n_name = "2199";

String sql = "select * from teacherinfo";

String sql1 = "update teacherinfo set name=\"Mr Liu\" where name=\"Mr Wang\"";

?

int rs1 = st.executeUpdate(sql1);

if(0 !=rs1)

{

System.out.println("是否更新成功:"+"yes");

}

else

{

System.out.println("是否更新成功:"+"No");

}

ResultSet rs = st.executeQuery(sql);

?

while(rs.next())

{

String name = rs.getString(1);

String code = rs.getString(2);

String sexy = rs.getString(3);

String age = rs.getString(4);

String birthday = rs.getString(5);

String address = rs.getString(6);

String salary = rs.getString(7);

System.out.println("name:"+name+" code:"+code+" sexy"+sexy+" age"+age+" birthday"+birthday+" address" +

address+" salary"+salary);

}

rs.close();

con.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驱动程序: Class.forName("com.mysql.jdbc.Driver");

* 获取连接类Connection的对象: Connection

* */

Java connect to MySQL through JDBC

标签:

原文地址:http://www.cnblogs.com/shunalone/p/4700130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!