标签:jdbc
首先创建一个数据库test1,再在数据库test1下创建一个表user
打开命令行 输入 mysql -h 127.0.0.1 -u root -p ---->再输入密码 登陆了MySQL
输入 CREATE DATABASE test1;
输入USE test1;
输入
CREATE TABLE user( a varchar(20), b varchar(20), c varchar(20) );再插入几组数据
INSERT INTO user (a,b,c)VALUES(‘lisi‘,‘3001‘,‘100‘);
INSERT INTO user (a,b,c)VALUES(‘lihua‘,‘3002‘,‘150‘);
INSERT INTO user (a,b,c)VALUES(‘zhangsan‘,‘3003‘,‘200‘);
一个简单的表建立了
---------------------------------------------------------------------------------------------------------------------------------------
下面是java程序:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class test1 { public static void main(String[] args) throws Exception { test(); } static void test() throws SQLException{ //1注册驱动 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); String url="jdbc:mysql://localhost:3306/test1"; String user ="root"; String password=""; //建立连接 Connection conn = DriverManager.getConnection(url,user,password); //创建语句 Statement st =conn.createStatement(); //执行语句 ResultSet rs =st.executeQuery("select * from user"); while(rs.next()){ System.out.println((String)rs.getObject(1)+rs.getObject(2)+rs.getObject(3)); } //关闭连接 rs.close(); st.close(); conn.close(); } }运行后结果为
lisi3001100
lihua3002150
zhangsan3003200
标签:jdbc
原文地址:http://blog.csdn.net/u013107634/article/details/39402969