JDBC连接MySQL

上传人:re****.1 文档编号:508432050 上传时间:2022-10-31 格式:DOC 页数:18 大小:78.50KB
返回 下载 相关 举报
JDBC连接MySQL_第1页
第1页 / 共18页
JDBC连接MySQL_第2页
第2页 / 共18页
JDBC连接MySQL_第3页
第3页 / 共18页
JDBC连接MySQL_第4页
第4页 / 共18页
JDBC连接MySQL_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《JDBC连接MySQL》由会员分享,可在线阅读,更多相关《JDBC连接MySQL(18页珍藏版)》请在金锄头文库上搜索。

1、JDBC连接MySQL经典方案最近在学习数据库开发的一些实例,这里浅谈一下用JDBC连接数据库MySQL(当然也可以连接SQL Sever或Oracle了,只是我更喜欢开源软件,同时也更简单)。首先正确安装好MySQL,建立好数据库studentinfomysqlcreate database studentinfo;然后编写java代码,ConnectToMySQL.javaimport java.sql.*;public class ConnectToMySQL public static Connection getConnection() throws SQLException ,ja

2、va.lang.ClassNotFoundExceptionString url = jdbc:mysql:/localhost:3306/studentinfo;Class.forName(com.mysql.jdbc.Driver);String userName = root;String password = ;Connection con = DriverManager.getConnection(url,userName,password);return con;public static void main(String args) tryConnection con = get

3、Connection();Statement sql = con.createStatement();sql.execute(drop table if exists student);sql.execute(create table student(id int not null auto_increment,name varchar(20) not null default name,math int not null default 60,primary key(id););sql.execute(insert student values(1,AAA,99);sql.execute(i

4、nsert student values(2,BBB,77);sql.execute(insert student values(3,CCC,65);String query = select * from student;ResultSet result = sql.executeQuery(query);System.out.println(Student表数据如下:);System.out.println(-);System.out.println(学号+姓名+数学成绩);System.out.println(-);int number;String name;String math;w

5、hile(result.next()number = result.getInt(id);name = result.getString(name);math = result.getString(math);System.out.println(number + + name + + math);sql.close();con.close();catch(java.lang.ClassNotFoundException e)System.err.println(ClassNotFoundException: + e.getMessage();catch(SQLException ex)Sys

6、tem.err.println(SQLException: + ex.getMessage();要注意的是使用MySQL数据库,需要用到对应的JDBC驱动程序mysql-connector-java-5.0.3import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.ResultSet;import java.sql.Statement;import java.sql.*;public class BaseConnection private Con

7、nection con = null;protected Connection getCon() ResultSet rs=null; Statement stmt = null;try Class.forName(org.gjt.mm.mysql.Driver); String url=jdbc:mysql:/192.168.0.10/数据库名?user=USR&password=PWD;conn = DriverManager.getConnection(url); stmt = conn.createStatement(); catch (ClassNotFoundException e

8、) e.printStackTrace(); catch (SQLException e) e.printStackTrace(); return con;1.把mysql-connector-java-5.0.3-bin.jar放到%JAVA_HOME%jrelibext下2.访问类:package mysqldb;import java.sql.*;public class MysqlCon private static String DriverName = org.gjt.mm.mysql.Driver;private String dbURL = jdbc:mysql:/localh

9、ost/test;private String dbuser = root;private String dbpassword = ;private Connection conn;private Statement stmt;private ResultSet rs;public MysqlCon()try Class.forName(DriverName).newInstance();conn = DriverManager.getConnection(dbURL,dbuser,dbpassword);stmt = conn.createStatement();String sql = s

10、elect * from worker;rs = stmt.executeQuery(sql);while(rs.next()System.out.println(rs.getString(1); catch (InstantiationException e) / TODO 自动生成 catch 块e.printStackTrace(); catch (IllegalAccessException e) / TODO 自动生成 catch 块e.printStackTrace(); catch (ClassNotFoundException e) / TODO 自动生成 catch 块e.p

11、rintStackTrace(); catch (SQLException e) / TODO 自动生成 catch 块e.printStackTrace();package interphase;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.

12、swing.table.AbstractTableModel;import mysqldb.MysqlCon;public class ShowRS private AbstractTableModel atm;private JTable jtable;private Vector vector;private JScrollPane jsp;private String title=职工号,职工名,性别,出生日期,工资;private MysqlCon mysqlcon;private JFrame frame;public ShowRS()vector = new Vector();at

13、m = new AbstractTableModel()public int getColumnCount() return title.length;public int getRowCount() return vector.size();public Object getValueAt(int rowIndex, int columnIndex) if(!vector.isEmpty()return (Vector)vector.elementAt(rowIndex).elementAt(columnIndex);else return null;/取得单元格中的属性值 public String getColumnName(int columnIndex)return titlecolumnIndex;/数据模型不可编辑,该方法设置为空 public void setValueAt()/取得列所属对象类 public Class getCoumnClass(int c)return getValueAt(0,c).getClass();/设置单元格不可

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号