首页 > 编程知识 正文

java动态创建mysql表,用mysql创建表

时间:2023-12-27 22:27:04 阅读:325455 作者:GFYQ

本文目录一览:

如何用java创建mysql数据库

JDBC连接数据库

•创建一个以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=truecharacterEncoding=gbk ;

useUnicode=true:表示使用Unicode字符集。如果characterEncoding设置为

gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式。

3、创建数据库的连接

•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象,

该对象就代表一个数据库的连接。

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

String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和

密码来获得。

例如:

//连接MySql数据库,用户名和密码都是root

String url = "jdbc:mysql://localhost:3306/test" ;

String username = "root" ;

String password = "root" ;

try{

Connection con =

DriverManager.getConnection(url , username , password ) ;

}catch(SQLException se){

System.out.println("数据库连接失败!");

se.printStackTrace() ;

}

4、创建一个Statement

•要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3

种类型:

1、执行静态SQL语句。通常通过Statement实例实现。

2、执行动态SQL语句。通常通过PreparedStatement实例实现。

3、执行数据库存储过程。通常通过CallableStatement实例实现。

具体的实现方式:

Statement stmt = con.createStatement() ;

PreparedStatement pstmt = con.prepareStatement(sql) ;

CallableStatement cstmt =

con.prepareCall("{CALL demoSp(? , ?)}") ;

java语言如何动态循环创建mysql数据库基本表,可以实现吗?

下面是一个简单的连接MySQL数据库,并操作数据库表的例子:

import java.sql.*;

public class TestMysql {

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

//加载驱动

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

//创建连接

conn = DriverManager

.getConnection("jdbc:mysql://localhost/bbs?user=用户名password=密码");

stmt = conn.createStatement();

rs = stmt.executeQuery("select * from user");

while (rs.next()) {

String user = rs.getString(1);

String password = rs.getString(2);

System.out.println("用户名:" + user + "," +" 密码:" + password );

}

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

} catch (SQLException ex) {

ex.printStackTrace();

} finally {

try {

if (rs != null) {

rs.close();

rs = null;

}

if (stmt != null) {

stmt.close();

stmt = null;

}

if (conn != null) {

conn.close();

conn = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

当然前提是要把MySQL的连接驱动JAR文件添加到工程里

Java开发工程师如何在Mysql数据库中创建表

package com.runoob.test;

import java.sql.*;

public class MySQLDemo {

// JDBC 驱动名及数据库 URL

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";

// 数据库的用户名与密码,需要根据自己的设置 static final String USER = "root"; static final String PASS = "123456";

public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{

// 注册 JDBC 驱动 Class.forName("com.mysql.jdbc.Driver");

// 打开链接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS);

// 执行查询 System.out.println(" 实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM websites"; ResultSet rs = stmt.executeQuery(sql);

// 展开结果集数据库 while(rs.next()){

// 通过字段检索 int id = rs.getInt("id"); String name = rs.getString("name"); String url = rs.getString("url");

// 输出数据 System.out.print("ID: " + id); System.out.print(", 站点名称: " + name); System.out.print(", 站点 URL: " + url); System.out.print("n");

}

// 完成后关闭 rs.close(); stmt.close(); conn.close();

}catch(SQLException se){

// 处理 JDBC 错误 se.printStackTrace();

}catch(Exception e){

// 处理 Class.forName 错误 e.printStackTrace();

}finally{

// 关闭资源 try{ if(stmt!=null) stmt.close();

}catch(SQLException se2){

}// 什么都不做 try{ if(conn!=null) conn.close();

}catch(SQLException se){ se.printStackTrace();

}

} System.out.println("Goodbye!");

}

}

如何用java创建MYSQL的数据表?

class.forname("oracle.jdbc.driver.OracleDriver");//加载数据库驱动

String url="jdbc:oracle:thin:@localhost:1521:db_name";

String sql="CREATE TABLE table(filed1 varchar2(2),filed2 varchar2(2))";

Connection conn=DriverManager.getConnection(url,"scott","tiger");//建立数据库连接

if(!conn.isClose()){

Statement stmt = conn.createStatement();

stmt.executeUPDATE(sql); //建立一个表

}

java 创建MySQL表

create table UserInfo( 创建 表 表名 这里create table是固定写法,表名自己起

id int parmary key, 列名,数据类型。parmary key表示该列为主键列

name varchar(20) not null, 列名,数据类型(数据长度)。not null表示该列不允许为空

age int not null 这个同上

)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。