标签:
And then Extract to "mysql-connector-java-5.0.8" file.
?
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");
?
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